1

I have a php script which grabs an image from an external URL, reads it and saves it into a directory on my server. The script is located in a php file and contains :

<?php 
$image_url = "http://example.com/image.jpg"; 
$ch = curl_init(); 
$timeout = 0; 
curl_setopt ($ch, CURLOPT_URL, $image_url); 
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout); 

// Getting binary data 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1); 

$image = curl_exec($ch); 
curl_close($ch); 

$f = fopen('/home1/path/public_html/path/saved/image.jpg', 'w');
fwrite($f, $image);
fclose($f);
?>

Everything works great there...

What I would like to do is have the script do it for multiple URLs. The URLs would be written in a form textarea, separated by comas (or else).

A submit button would then tell the script to do the trick with all the URLs in the form and save them with whatever name, it's not important (random would do fine).

I'm still a newbie, and I'm learning PHP.

Thanks in advance for your help !

EDIT

My code looks like this now :

<?php 
error_reporting(E_ALL);
$image_urls = explode('\n', $_POST['urls']); 



foreach ($image_urls as $image_url) {
$ch = curl_init(); 
$timeout = 0; 
curl_setopt ($ch, CURLOPT_URL, $image_url); 
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout); 

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1); 

  $image = curl_exec($ch); 
  curl_close($ch); 

  $f = fopen('/home1/path/public_html/path/saved/'.rand().time().".jpg", 'w');
  fwrite($f, $image);
  fclose($f);

}
?>

It works only for the first one, and doesn't return any errors... Any ideas ?

Thanks for your great help !

14
  • Storing individual urls in a txt or db? Commented Mar 28, 2011 at 11:35
  • Storing in a textarea, divided by lines... Commented Mar 28, 2011 at 12:43
  • Sorry i totally overlooked the problem. Somehow i thought you want to fetch these images via a cron job. Commented Mar 28, 2011 at 12:46
  • Well I might like to do that later but for now I'm just trying to do that only... Any ideas on my problem ? Commented Mar 28, 2011 at 12:48
  • Do you mean how to fetch them via a cron job? Commented Mar 28, 2011 at 12:53

1 Answer 1

1

You need to extract the urls from a text-area and then loop over that:

<?php 
$image_urls = explode('\n', $_POST['urls']); # Will create a list of urls, if each line contains one url.

#Basic settings and initializers need to be ran only once. 
$sequencer = 1;
$timeout = 0;

foreach ($image_urls as $image_url) {
  $ch = curl_init(); 

  curl_setopt ($ch, CURLOPT_URL, $image_url); 
  curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout); 

  // Getting binary data 
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
  curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1); 

  $image = curl_exec($ch); 
  curl_close($ch); 

  $f = fopen("/home1/path/public_html/path/saved/image_$sequencer.jpg", 'w');
  fwrite($f, $image);
  fclose($f);
  $sequencer++;
}
?>

Obviously, you should clean out, validate and doublecheck the inputted information: not only to avoid Goatses, but also to avoid entries that break your application (such as white lines).

Sign up to request clarification or add additional context in comments.

8 Comments

Great thanks for your fast answer ! One more thing, in the HTML, I should set the action to "MYSCRIPT.php", and name the textarea "urls" right ?
Yes, method="post" action="myscript.php" with a textarea "urls" Be aware that anyone (also those without access to the code that renders the textarea) can send a POST with a list of urls. You may want to check for access first in above-mentioned code. Also, see Hassan's answer for further improvements: putting such a saver in a function makes better code, more readable, better maintainable and less bug-prone.
So I tried with your solution, and when I put two urls in the textarea on different lines, I get a file called image_1.jpg, with a weight of 0kb. And that's it. Any Idea of what I'm doing wrong ? Thanks !
Ok, now it works, but it does it for the first url only... Thoughts ?
I changed the code: there was a mistake in closing-opening the curl-resource. For future: try debugging and investigate logs. The logs would have told you the problem here. Debugging would have shown you why the problem occured. Sprinkle some var_dump() over your code. Like a var_dump($image_urls) just after your created that, to see if it is a correct list of urls (Array).
|

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.