Page 1 of 1

Copying .gif from server to hd

Posted: Sat Dec 06, 2003 2:28 pm
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?

Posted: Sun Dec 07, 2003 9:00 am
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.

Posted: Sun Dec 07, 2003 1:54 pm
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.