Forum Moderators: coopster

Message Too Old, No Replies

saving remote files locally in PHP

         

scorpion

7:24 pm on Apr 23, 2003 (gmt 0)

10+ Year Member



Suppose you have a remote image file at:
[remoteserver.com...]

Does anybody know a PHP code that will "download and save" this file to your server? Does your server directory have to have a particular chmod permission too?

DrDoc

7:34 pm on Apr 23, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You can use something like fopen [php.net]...

And yes, you need write permissions for the folder in question...

scorpion

7:55 pm on Apr 23, 2003 (gmt 0)

10+ Year Member



yes but fopen opens a remote file for reading and then you can say echo it or something, how do you physically save the file to your server so that later on you can refer to it from their? I don't mean like a cache, I mean you specify the location on your server where the file is to be saved...

toadhall

8:02 pm on Apr 23, 2003 (gmt 0)

10+ Year Member



You could use the GD library (if it's available on your server) for pngs and jpegs. Major limitation is the latest versions of GD won't handle gifs due to copyright. But,...

ImageCreateFromPng() [php.net], ImagePng()
ImageCreateFromJpeg(), ImageJpeg()
ImageDestroy()

T

DrDoc

8:03 pm on Apr 23, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Use fopen [php.net] in combination with fwrite [php.net]

DrDoc

8:05 pm on Apr 23, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Also, check the links on the left side...

scorpion

8:31 pm on Apr 23, 2003 (gmt 0)

10+ Year Member



fwrite seems to write strings to a file pointer...can it write an image to a file pointer? What I want to do is save a file opened using fopen to my local server through PHP.

fwrite($localfilehandle, "http://www.remoteserver.com/image.jpg") will work?

DrDoc

8:40 pm on Apr 23, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



First you must open the local file for writing, using fopen [php.net]. Then you can retreive the file using readfile [php.net], file [php.net], fputs [php.net]... whichever you like best. Just write the data to the local file, using fwrite [php.net], and close the file using fclose [php.net].

There's a simple example on the fwrite page. You have other examples on the file and fopen pages.

scorpion

8:58 pm on Apr 23, 2003 (gmt 0)

10+ Year Member



oh I see, so you read the JPG file line by line as a string then put it to the stream because it is binary-safe..good idea!

grahamstewart

11:13 pm on Apr 23, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You don't read it line by line, you read it chunk by chunk.
Use
fread( $fp, 4096 )
or
fgets( $fp, 4096 )

And make sure that you specify 'rb' when fopening the input file and 'wb' when fopening the output. The 'b' sets it to binary mode - which can matter on some systems.

grahamstewart

11:14 pm on Apr 23, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Also see this recent thread [webmasterworld.com]