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]

 

 

 

Link to comment
Share on other sites

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!
}
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.