1

I'm saving and resizing tons of images from a remote server using cURL in PHP, but the previous developer doesn't implemented any kind of system for make the user uploaded pictures "safe", so the users were able to upload any kind of file format with any kind of name then the uploaded files name is saved to a database as is, so lots of files have non URL safe and iso-8859-8 characters. For example:

gaght101125659_5 Gn-eŐs mtó gÁrlós.jpg

According to this answer I made a code for getting the pictures.

private function getRemoteUrl($file)
{
  $ch = curl_init();
  curl_setopt($ch, CURLOPT_URL, $file);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
  $data = curl_exec($ch);
  curl_close($ch);
  return $data;
}

$img = imagecreatefromstring($this->getRemoteUrl($url));//$url contains the plain url of the image.

This code works perfectly for images with a name like gaht123115516_2.jpg but for the example shown above it gives an error saying:

Warning: imagecreatefromstring() [function.imagecreatefromstring]: Data is not in a recognized format in /home/test/public_html/resizing.php on line 64

Of course, because of the fancy characters. So what should I do in getRemoteUrl($file) with the $file variable to make it work? Can this be done in PHP? If not what other methods/programming languages should I try?

Community
  • 1
  • 1
totymedli
  • 29,531
  • 22
  • 131
  • 165
  • 2
    You need to URL-encode `$file`, but only the file name portion. You can't URL-encode the entire URL because it will change `http://` into `http%3A%2F%2F`. – quietmint Dec 25 '12 at 22:53

2 Answers2

1

URL encode the specified filename.

Matt Ball
  • 354,903
  • 100
  • 647
  • 710
1

Use urlencode() on the filename. don't urlencode() the entire url.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
goat
  • 31,486
  • 7
  • 73
  • 96
  • Yes, I tried to *urlencode()* the whole URL that is wrong as you said. Now I encoded it with only the filename, but it was still wrong. After this I realized that this method encodes spaces for a + character instead of %20. After an *str_replace()* it workes like a charm. Thanks. – totymedli Dec 25 '12 at 23:33