I am using a wordpress plugin called "Add Linked Images To Gallery" in order to save a local copy of images posted on my site. I uploaded all my files in a different hosting and I was testing this plugin in localhost to see if it does the job .

What does this plugin do :
The plugin searches for external images posted in the site , save a local copy and replace the old image address in HTML code with the new image address which is in the local.

What is the problem :
While testing the plugin in the local host , I noticed that this plugin does not correctly save images which have "white spaces" on their file names. For example :

This is an Image.png

will not display in the blog, instead this is the correct way to show it :


as you can see , the white spaces are replaced with "%2520" , I am not sure why this is happening, but the image wont show up in the blog.

I tried to modify this plugin so it automatically replaces the white space in the image file with "_" , I look at the source code and found this line:

    $content = preg_replace('/(<img[^>]* src=[\'"]?)('.$trans.')/', '$1'.$imgpath, $content);

which I believe is responsible for the replacing images, but when I modified it , it removed the post content and image.

Is it possible to modify this plugin, so it replace the white space in the file name first , then save a local copy, then use that local copy in the blog ?


  • That piece of code is to remove everiting from an image tag <img src="example.com"> and get the url, nothing to do with the file name
    – user983248
    Commented May 24, 2012 at 7:15

2 Answers 2


Use sanitize_file_name( $filename ). This function doesn't just catch white space, it removes other special characters too:

$special_chars = array(
    "?", "[", "]", "/", "\\", "=", "<", ">", ":", ";", ",", 
    "'", "\"", "&", "$", "#", "*", "(", ")", "|", "~", "`", 
    "!", "{", "}", chr(0)

Find the following line in the plugin's code:

$id = media_handle_sideload($file_array, $post_id, $desc);

Immediately above this line add:

$file_array['tmp_name'] = sanitize_file_name( $file_array['tmp_name'] );

Caveat: I haven't tested it.

Make sure to rename the plugin and the plugin's file name. Otherwise you get upgrade notices. And contact the author, ask him to implement these changes.

  • should I place this code in the theme function or in the plugin? thanks
    – Dave
    Commented May 24, 2012 at 12:01
  • I just realized this function is included in wordpress, the question is where should I use this function so it replaces the whitespaces with dash before that plugin save it in a local storage.
    – Dave
    Commented May 24, 2012 at 12:07
  • @Dave See my update.
    – fuxia
    Commented May 24, 2012 at 17:41
  • thanks again for the respond - I tried the code but unfortunately it still save the image with whitespaces in the name, and the picture wont show up on the site,
    – Dave
    Commented May 24, 2012 at 19:45
  • any other solution ?
    – Dave
    Commented May 25, 2012 at 13:55

Try replacing:

rename($file_array['tmp_name'], $file_array['tmp_name'] . $ext);


$newname = strtolower(str_replace(' ', '-', $file_array['tmp_name']));
rename($file_array['tmp_name'], $newname . $ext);

Not tested

  • unfortunately it did not work, it still save the image with the whitespaces in the file name, and does not replace it with a dash line
    – Dave
    Commented May 24, 2012 at 12:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.