[wp-trac] [WordPress Trac] #21244: media_sideload_image() should replace spaces and %20's in URLs to dashes

WordPress Trac wp-trac at lists.automattic.com
Thu Jul 12 17:07:56 UTC 2012


#21244: media_sideload_image() should replace spaces and %20's in URLs to dashes
--------------------------+-----------------------------
 Reporter:  kingkool68    |      Owner:
     Type:  defect (bug)  |     Status:  new
 Priority:  normal        |  Milestone:  Awaiting Review
Component:  Media         |    Version:
 Severity:  normal        |   Keywords:
--------------------------+-----------------------------
 I wrote a script to import content by scraping the HTML from a site. Yuck!
 Some of the image file names have spaces in them. DOUBLE YUCK!

 I needed to use `media_sideload_image()` to import them into WordPress'
 media library.  But the only way to successfully download the image is to
 first replace the space characters with %20. Then WordPress saves the new
 filename with %20's in them and WordPress multisite's .htaccess rules
 despise filenames with %20 in them.

 Here is how I fixed it:

 `$file_array['name'] = basename($matches[0]);`

 becomes

 `$file_array['name'] = preg_replace('/\s|%20/', '-',
 basename($matches[0]));`

 Need an image with a space in the filename? Use this
 http://www.pewforum.org/uploadedImages/Topics/Issues/Social_Welfare/John%20DiIulio2.jpg

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


More information about the wp-trac mailing list