1

I know my question is quite similar to this question and I'm using the answer to the problem as my solution but I can't get it to work.

I'm using Immediatenet.com to generate a thumbnail of any given URL (example: google). And then using cURL to save the image in a folder on my server.

I'm not sure if the problem lies in the fact that the URL isn't actually an image URL (ie example.com/image.jpg) or a .html/.php URL or that I'm simply not doing it right. I've never used cURL before so it's distinctly possible that I'm not doing it right.

I'm saving the path to the image into a MySQL database which works just fine. However, the image doesn't actually save in the folder specified. The permissions are set correctly on the folder, so I don't think it's a permissions problem. I also set allow_url_fopen to On just to make sure.

My code is below. Can anyone point me in the right direction for this one?

$url = $_POST['url'];
$url_new = 'http://immediatenet.com/t/m?Size=1024x768&URL='.$url;
$img = "/users/images/".$url.".jpg";
$ch = curl_init($url_new);
$fp = fopen($img, 'wb');
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_exec($ch);
curl_close($ch);
fclose($fp);
3
  • 2
    What's the value of $url? If it contains / characters, $img will be a path into a subdirectory, and you need to ensure that those subdirectories exist. Check for errors from fopen(). Commented Dec 31, 2012 at 17:08
  • E.g. if $url is http://google.com, you need to have a directory /users/images/http:. Commented Dec 31, 2012 at 17:12
  • Oh wow. I'm such an idiot. I just realised that you're totally correct. Those forward slashes are messing it up. Thank you. You should put this as the answer and I'll accept... begrudgingly because I feel like an idiot now. And hopefully get this question closed. Thank you so much. You've saved me hours of headaches. Commented Dec 31, 2012 at 17:12

2 Answers 2

2

If $url contains forward slashes (as most URLs do), these will be treated as directory delimiters in the $img pathname. You either need to replace the slashes with some other character, or create all the necessary directories.

$img = "/users/images/".str_replace('/', '_', $url).".jpg";
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks again, and for future reference if anyone is having the same problem. I also had a problem whereby it wouldn't save using "/users/...". I had to change it to "./users/...".
0

Are you sure $img = "/users/images/".$url.".jpg"; is valid? .. do you really have a users folder in your root?

Please correct that if not and it should be ok.

2 Comments

There's a users folder for sure. I've saved images into that images folder before (for avatar uploads) but this one doesn't seem to work.
By root I mean filesystem root (you are on unix-type) not your webroot.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.