Jump to content

[SOLVED] Downloading external images to server


Skyre

Recommended Posts

I'm trying to write a script that simply downloads the daily Ziggy comic. The image URL for today's comic is http://images.ucomics.com/comics/zi/2007/zi070625.gif and the six digits in the filename are today's date.

 

This is what I've written out so far, but it doesn't work. A file gets written, but what gets returned is a HTML page with a 404 error on it.

 

[pre]$filename = $_SERVER['DOCUMENT_ROOT']."/comics/ziggy.gif";

 

if ($fi=fsockopen("images.ucomics.com",80)) {

fputs($fi,"GET /comics/zi/2007/zi070625.gif HTTP/1.0\r\n\r\n");

while(!preg_match("/^\r?\n$/",fgets($fi,1024))); //skip headers

 

if($fo=fopen($filename,"w")) {

while(!feof($fi)) {

fwrite($fo,fread($fi,65535));

}

fclose($fo);

}

fclose($fi);

}[/pre]

 

 

 

If you turn on fopen wrappers to allow for passing URLs to your file manipulation functions, you may be best off simply using the copy() function:

<?php
$filename = 'zi' . date('ymd') . '.gif';
$new = "/path/to/my/files/{$filename}";
if (!copy("http://images.ucomics.com/comics/zi/2007/{$filename}", "$new)) {
  // error copying file!
}
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.