Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Save image from url to local/remote directory 1

Status
Not open for further replies.

Brianfree

Programmer
Feb 6, 2008
220
GB
Hi, im trying to figure out how to save an image from a url to a local directory on my computer or a remote directory where the php page is run from..

I have a text file with a list of image names like…

1000.gif
1001.gif
1145.gif

i want to be able to download each of these images one at a time from a url like…

http:\\
If the image is not available it is not downloaded and the next one in the list is attempted.


Is this possible?

Many thanks,

Brian Freemantle
 
Hi, i have put together the following code which works fine but times out after 30 seconds. Is it possible to say do 100 at a time, delete the line out of the txt file and run again until the whole text file has been downloaded? Also, if the image does not exist dont attept to download it?

Code:
<?php
$file = "images.txt";
$f = fopen($file, "r");

while ($line = fgets($f, 100)){
$line = trim($line);

//Get the file
$content = file_get_contents("[URL unfurl="true"]http://www.website/images/".$line.".gif");[/URL]

//Store in the filesystem.
$fp = fopen("downloads/".$line.".gif", "w");
fwrite($fp, $content);
fclose($fp);
}
?>

Many thanks,

Brian
 
Code:
$url = '[URL unfurl="true"]http://www.website.com/images/';[/URL] //note trailing slash
$images = array('1000.gif', '1001.gif', '1002.gif')'
$destDir = '/path/to/my/writable/directory/';
//EITHER
foreach ($images as $image):
  $n = uniqid('downloaded_file_', true);
  copy($url.$image, $destDir . $n . $image);
endforeach;

//OR
foreach ($images as $image):
  $n = uniqid('downloaded_file_', true);
  $f = file_get_contents($url . $image);
  if($f) file_put_contents($f, $destDir . $n . $image);
endforeach;

//or you could you fopen commands or you could use curl or you could use a system call to wget

please be sure that your extraction and use of the remote images is wholly lawful and not an infringement of the remote site's copyright licence terms.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top