tags:

views:

58

answers:

4

How?

I think I've done this with .PDFs at one time in the past, but I lost the code, and now nothing is turning up on the web about how to go to a URL, get a .gif and copy the .gif file into my directory.

Thanks for any help.

+4  A: 
$gif = file_get_contents("http://path.to/gif/pic.gif");
file_put_contents("path/on/server",$gif);

You need to have fopen wrappers enabled for this to work.

Byron Whitlock
As a side note, if the GIF is enormous (>1MB), you may want to consider doing this with `fopen` and downloading in chunks. If you don't, you could get an error that PHP is out of memory.
mattbasta
@matbasta, Very true but it would have to be alot bigger than 1MB. Doesn't PHP default to like 64MB ?
Byron Whitlock
and very few gif are bigger than 1MB
mathroc
Thanks. Will be working on the code this morning.
A: 

if your php configuration allow you to fopen url (allow_url_fopen directive in php.ini)

file_put_contents ($filename, file_get_contents ($url));

mathroc
Thanks. Will be working on the code this morning.
A: 

You'll need to use CURL, or file_get_contents.

Rich Bradshaw
Thanks. Will be working on the code this morning.
+1  A: 

http://www.php.net/copy

copy() can use use a url as the source.

chris
Oh yes thats realy help full a big -1 from me
streetparade
Congrats. You've downvoted the best answer.
chris
Why won't copy work? You can use it with streams as of 4.3. What's wrong with this answer?
easement
You are welcome. Sorry but there is no best answer. Every answer here has a solid basis to solve this simple task.It depends on the developer, which is usable and which not.If you are because of that insulted, i can also vote it up for you, just edit your answer.
streetparade
copy() is definitely superior to file_put_contents() in this context, as it should operate on a file of any with minimal memory usage. cURL should also behave comparably if correctly configured, and may actually perform better than copy().
Frank Farmer
Thank you for these answers. I'll be working on the code this morning.