Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 428
user2505513 Avatar asked Feb 15 '23 04:02

user2505513


1 Answers

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' )
);
like image 87
Damiano Mologni Avatar answered Feb 16 '23 18:02

Damiano Mologni