MauJFernandez
MauJFernandez

Reputation: 346

Download image from a web (direct link) with PHP

Hey, I've got a bunch of URLs in a .txt file, lets say:

www.example.com/image1.png
www.example.com/image2.png
www.example.com/image3.png
www.example.com/image4.png
www.example.com/image5.png
...
www.example.com/image900.png

I want, with PHP, to read that list and save those images to a folder.
Reading that list line by line is easy, but I've got no idea how to save them to disk.

Thanks!

Upvotes: 1

Views: 1209

Answers (4)

Billy Moon
Billy Moon

Reputation: 58521

I imagine it might be possible like this:

file_put_contents('/dest/file.png',file_get_contents('http://src.com/image.png'));

Upvotes: 2

fabrik
fabrik

Reputation: 14365

Try file_get_contents() to fetch images then fwrite() to 'save' them.

Upvotes: 0

Theresa Forster
Theresa Forster

Reputation: 1932

I would use Perl rather than php as PHP needs a webserver and perl can do it directly

but you can use Curl as follows

$ch = curl_init('http://example.com/image.php');
$fp = fopen('/my/folder/flower.gif', 'wb');
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_exec($ch);
curl_close($ch);
fclose($fp);

Upvotes: 0

bensiu
bensiu

Reputation: 25564

use combination of file_get_contents() and file_put_contents()

Upvotes: 0

Related Questions