Copying .gif from server to hd

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
nigma
DevNet Resident
Posts: 1094
Joined: Sat Jan 25, 2003 1:49 am

Copying .gif from server to hd

Post by nigma »

Hey,

I am attempting to use the following script to download a gif from a server, then put it into a local file, but when I view the local version of the gif it is all "scrambled."

My Code:

Code: Select all

<?php
$newPath = "LOCALPATHHERE";

$url	= "URLHERE";
$fileDir	= "01/";
$filePre	= "ch";
$fileMid	= 920101;
$filePost	= ".gif";

for ( ; $fileMid <= 920105; $fileMid++)
{
	$newFile	= $newPath . $filePre . $fileMid . $filePost;
	$getFile 	= $url . $fileDir . $filePre . $fileMid . $filePost;
	
	$fp1 	= fopen($getFile, "r") or die ("Couldn't open $getFile.");
	$fp2 	= fopen($newFile, "w") or die ("Couldn't open $newFile."); 

	while (!feof($fp1))	{ fwrite($fp2, fgets($fp1, 1024)); }

	fclose($fp1);
	fclose($fp2);
}
?>
The reason I split the filename up so much is because there are about 8 dirs, numberd 01 - 08, each dir contains a certain number of files with a number inside the filename that grows larger, and I would eventually like to loop through all dirs and all .gifs in the dirs and save them.

Any ideas on what is going on and how to fix it?
Gen-ik
DevNet Resident
Posts: 1059
Joined: Mon Aug 12, 2002 7:08 pm
Location: London. UK.

Post by Gen-ik »

Not sure how your computer is setup (do you have Apache?) but have you tried using copy().
int copy ( string source, string dest)


Makes a copy of a file. Returns TRUE if the copy succeeded, FALSE otherwise. Example 1. copy() example

if (!copy($file, $file.'.bak')) {
print ("failed to copy $file...<br>\n");
}




Note: As of PHP 4.3.0, both source and dest may be URLs if the "fopen wrappers" have been enabled. See fopen() for more details.
Just a thought.
User avatar
nigma
DevNet Resident
Posts: 1094
Joined: Sat Jan 25, 2003 1:49 am

Post by nigma »

I did see it when looking through the manual, but never thought to use it. Thanks a bunch, I give it a shot.
Post Reply