[wp-trac] [WordPress Trac] #21116: can't upload file with spaces in its name with function media_sideload_image

WordPress Trac wp-trac at lists.automattic.com
Fri Jun 29 21:17:24 UTC 2012


#21116: can't upload file with spaces in its name with function
media_sideload_image
--------------------------+-----------------------------
 Reporter:  blatan        |      Owner:
     Type:  defect (bug)  |     Status:  new
 Priority:  normal        |  Milestone:  Awaiting Review
Component:  Media         |    Version:  3.4
 Severity:  normal        |   Keywords:
--------------------------+-----------------------------
 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.

 Regards,
 Bernard

-- 
Ticket URL: <http://core.trac.wordpress.org/ticket/21116>
WordPress Trac <http://core.trac.wordpress.org/>
WordPress blogging software


More information about the wp-trac mailing list