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 !