0

I want to amend a PHP script I'm using in wordPress (Auto Featured Image plugin).
The problem is that this script creates filenames for thumbnails based on the URLs of the image.

That sounds great until you get a filename with spaces and the thumbnail is something like this%20Thumbnail.jpg and when the browser goes to http://www.whatever.com/this%20Thumbnail.jpg it converts the %20 to a space and there is no filename on the server by that name (with spaces).

To fix this, I think I need to change the following line in such a way that $imageURL is filtered to convert %20 to spaces. Sound right?
Here is the code. Perhaps you can tell me if I'm barking up the wrong tree.
Thank you!

<?php
  static function create_post_attachment_from_url($imageUrl = null)
  {
      if(is_null($imageUrl)) return null;
      // get file name
      $filename = substr($imageUrl, (strrpos($imageUrl, '/'))+1);
      if (!(($uploads = wp_upload_dir(current_time('mysql')) ) && false === $uploads['error'])) {
          return null;
      }
      // Generate unique file name
      $filename = wp_unique_filename( $uploads['path'], $filename );
?>
2
  • What about other characters?
    – Salman A
    Mar 24, 2012 at 6:46
  • @salman-a - what other characters are you talking about? Mar 26, 2012 at 8:20

2 Answers 2

1

Edited to a more appropriate and complete answer:

static function create_post_attachment_from_url($imageUrl = null)
{
    if(is_null($imageUrl)) return null;

    // get the original filename from the URL
    $filename = substr($imageUrl, (strrpos($imageUrl, '/'))+1);

    // this bit is not relevant to the question, but we'll leave it in
    if (!(($uploads = wp_upload_dir(current_time('mysql')) ) && false === $uploads['error'])) {
        return null;
    }

    // Sanitize the filename we extracted from the URL
    // Replace any %-escaped character with a dash
    $filename = preg_replace('/%[a-fA-F0-9]{2}/', '-', $filename);

    // Let Wordpress further modify the filename if it may clash with 
    // an existing one in the same directory
    $filename = wp_unique_filename( $uploads['path'], $filename );

    // ...
}
3
  • am I wrong in assuming that the problem is not with the line you're describing but with line 5 in my code sample? I thank you for your response but I don't think it answers my question. Thank you! Mar 26, 2012 at 8:00
  • I think your issue is not completely clear to me: are you trying to solve a bug or just fixing the minor inconvenience of having escaped %20 sequences in the filename of the saved thumbnail? As far as I can tell, the script takes the URL from an image from the Internet, fetch the content and save that image on disk to create an attachment in Wordpress. I've edited the code to remove any escaped character (like spaces, symbols, etc) from the original image file name and replace these characters by dashes. Mar 26, 2012 at 9:32
  • $filename = preg_replace('/%[a-fA-F0-9]{2}/', '-', $filename); - this line saved my life! thanks
    – danyo
    Oct 2, 2014 at 19:35
0

You better to replace the spaces in image name with underscores or hypens using regexp.

$string = "Google%20%20%20Search%20Amit%20Singhal"
preg_replace('/%20+/g', ' ', $string);

This regex will replace multiple spaces (%20) with a single space(' ').

3
  • codef0rmer - I don't understand your response. I'm not looking to replace spaces, per se. Maybe I wasn't clear with my question. I want to modify the above script, which gets the filenames as URLs with %20 substituted for spaces, with a script that takes those filenames that have %20 in them and replaces the %20s with dashes. Mar 26, 2012 at 8:00
  • I think when you upload an image, it does not replace spaces with some characters (eg. underscore or hypen) and the image url has %20 chars replacing spaces which you do not want. My suggestion would be to replace spaces with underscore/hypen while you upload an image instead of parsing %20 later. Anyways, I've updated my answer.
    – codef0rmer
    Mar 26, 2012 at 8:07
  • 1
    Sorry, but I believe the that regex is wrong: /%20+/ should be /(%20)+/, otherwise, you only replace stuff like %20 and %200, %2000, etc. Beside, the + should not be necessary, and the g modifier is not valid, if I'm not mistaken, preg_replace replaces all occurrences in the string anyway. Mar 26, 2012 at 9:39

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.