Forum Moderators: coopster

Message Too Old, No Replies

Hard to explain .. Image tranfering in php?

         

havoc

3:20 am on Apr 22, 2003 (gmt 0)

10+ Year Member



Hey guys ,

I have been programming php for a little while and i have come across a conundrum what i need to do for a site i am doing is transfer one image from one server to another .. By basically putting in the web address and the path to the pic . To basically download it to my server for a local copy so i can display it from my server to save the other guy bandwidth.

i have made up one script but it only seems to be able to do pics less then 60k or so .. it basically just opens the file puts the contents into a string and saves it as a jpg

I do not have accessto GD or anything so is there a way i can do it effectively?

grahamstewart

3:33 am on Apr 22, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The script you have already sounds okay, but don't try and read the file into one big string.

Instead, read a chunk then write a chunk, and repeat till the file is done.

i.e.


while (!feof($inFp) ) {
$data = fgets($inFp, 4096);
fwrite($outFp, $data);
}

Also beware of magic_quotes_runtime adding slashes to your files.

havoc

4:15 am on Apr 22, 2003 (gmt 0)

10+ Year Member



$webpage = " location of image here ";
$fr = fopen( $webpage, "r" ) or die("couldn't open webpage");

$fd = fopen( $webpage, "r" );
$contents = fread($fd, 99999999999);
fclose( $fd );

$File = fopen("images/latest/latest.jpg","w");

fwrite($File,"$contents" );
fclose($File);
echo "..Done";

this is what i have ... how would i make it write in chunks?

grahamstewart

4:29 am on Apr 22, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Pretty much by changing it to what I have above. :)

You want something more like this..

[pre]
$webpage = " location of image here ";
$fd = fopen( $webpage, "rb" )
or die("couldn't open webpage");

$File = fopen("images/latest/latest.jpg","wb");

while (!feof($fd) ) {
$contents = fread($fd, 4096);
fwrite( $File, $contents );
}

fclose( $fd );
fclose( $File );

echo "..Done";
[/pre]

You might want to think about adding some more error handling. If someone gets disconnected while they are transferring the file then you might end up with half a file. So it might be better to write it into a temporary file and only replace the main file when you have definitely transfered a complete file.

havoc

4:37 am on Apr 22, 2003 (gmt 0)

10+ Year Member



did that and i almost had a lawsuit on my hands ... :D

hehe i forgot to change

while (!feof($fd) ) {

$contents = fread($fd, 4096);

and i ended up getting in an inifsite loop in 2 mins my file was 1.65 gig

havoc

4:50 am on Apr 22, 2003 (gmt 0)

10+ Year Member



yes this is in my Update part of the site so a user will only see latest.jpg and so it will not be a user activated script.

Thanks grahamstewart it works poifect!