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 derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Copying images off of a site

Status
Not open for further replies.

CliffLandin

Programmer
Nov 14, 2004
81
US
I need to copy around 7,000 images from a site to my computer. I have a csv file with the image names and the path to the images.

I have tried using this...

Code:
<?php
$urlPath = "[URL unfurl="true"]http://pathtoimages/";[/URL]
$csvFile = "nameof.csv";
$cnt = 0;


$openCSV = fopen($csvFile, "r");

while (($data = fgetcsv($openCSV, 1000, ",")) != false){

	$fileName = $data[ 7 ];
	
	$imagePath = $urlPath.$fileName;
	
	$image = file($imagePath);

	$newImage = fopen($fileName, 'wb');

	fwrite($newImage, $image);

	fclose($newImage);
}
?>

but this creates empty images, I believe because file returns an array.

I'm sure that there is some way to go about this, but I really don't know what that might be.


When in doubt, go flat out!

 
Okay, I figured it out.

For any one that is curious;

Code:
<?php
$urlPath = "[URL unfurl="true"]http://pathtoimages/";[/URL]
$csvFile = "nameofcsv.csv";
$cnt = 0;


$openCSV = fopen($csvFile, "r");

while (($data = fgetcsv($openCSV, 1000, ",")) != false){
	if($cnt < 1){
	$fileName = $data[ 7 ];
	
	$imagePath = $urlPath.$fileName;
	
	$image = file($imagePath);
	foreach ($image as $line_num => $image){
	//	$line = rtrim($image);
		$mvImage .= $image;
	}

	$newImage = fopen($fileName, 'wb');
	fwrite($newImage, $mvImage);

	fclose($newImage);
	}
$cnt++;
}

?>

When in doubt, go flat out!

 
Just curious how efficient was this process? I use something called httrack to download globs of files off of websites.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top