Note that there are some explanatory texts on larger screens.

plurals
  1. POSave images from URLs in a form via php
    text
    copied!<p>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 : </p> <pre><code>&lt;?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); ?&gt; </code></pre> <p>Everything works great there...</p> <p>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). </p> <p>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). </p> <p>I'm still a newbie, and I'm learning PHP. </p> <p>Thanks in advance for your help !</p> <p><strong>EDIT</strong></p> <p>My code looks like this now :</p> <pre><code>&lt;?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); } ?&gt; </code></pre> <p>It works only for the first one, and doesn't return any errors... Any ideas ?</p> <p>Thanks for your great help !</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload