Make WordPress Core

Opened 12 years ago

Closed 12 years ago

#21116 closed defect (bug) (duplicate)

can't upload file with spaces in its name with function media_sideload_image

Reported by: blatan's profile blatan Owned by:
Milestone: Priority: normal
Severity: normal Version:
Component: Media Keywords:
Focuses: Cc:


When file has space(s) in its name it's impossible to upload it with media_sideload_image. I resolved it by adding few lines to the function.
I'm not expert in php so maybe you find a better way to fix it.
Below there is the part of the code which I changed.
I added comments with "blatan:" to make it easy to find.

function media_sideload_image($file, $post_id, $desc = null) {
	if ( ! empty($file) ) {
		// Download file to temp location
		// blatan: these lines are needed to load images with spaces in their names
		$parsed_file = parse_url($file);
		$path = $parsed_file['path'];
		$file_name = basename($path);
		$encoded_file_name = rawurlencode($file_name);
		$path = str_replace($file_name, $encoded_file_name, $path);
		$file = $parsed_file['scheme'] . "://" . $parsed_file['host'] . $path;
		echo "URL:$file"; 
		$tmp = download_url( $file );
		// Set variables for storage
		// fix file filename for query strings
		preg_match('/[^\?]+\.(jpg|JPG|jpe|JPE|jpeg|JPEG|gif|GIF|png|PNG)/', $file, $matches);
		// blatan: file name has to be decoded and sanitized to avoid problem with spaces in name
		// there are dashes instead spaces, so now in name there are two or more dashes next to each other 
		//$file_array['name'] = basename($matches[0]);
		$file_array['name'] = sanitize_file_name(rawurldecode(basename($matches[0])));
		$file_array['tmp_name'] = $tmp;

I hope this helps and hope it's enough problem to add the ticket.


Change History (1)

#1 @ocean90
12 years ago

  • Milestone Awaiting Review deleted
  • Resolution set to duplicate
  • Status changed from new to closed
  • Version 3.4 deleted
Note: See TracTickets for help on using tickets.