user2505513
user2505513

Reputation: 333

Downloading a file and saving it locally with PHP

I need to save a zipped file from an external url and save it as a temp file for use.

I've been advised to look at tempnam() and sys_get_temp_dir(), however I'm unsure how (or where) to include the url for the external file.

Can anybody help point me in the right direction as to the right function to use (and how)? I know how to unzip the file, it's just where to include the external url that points to the zipped file etc.

Upvotes: 3

Views: 11961

Answers (2)

Damiano Mologni
Damiano Mologni

Reputation: 221

You can simply use file_get_contents() to load the remote file and file_put_contents() to save the content to a temp file. You must have allow_url_fopen enabled in your php.ini

file_put_contents(
    'path/to/tmp/file.ext',
    file_get_contents( 'http://url_of_the_file.com/download.zip' )
);

Upvotes: 10

Momin
Momin

Reputation: 868

you can use file_put_contents file_put_contents("Tmpfile.zip", fopen("yoururlwithhttp/file.zip", 'r'));

The first argument is the file name that you want to save. If you want you can fetch it from the tmp folder or you can rename it with php with current timestamp

Hope it helps

Upvotes: 0

Related Questions