Search code examples
phpurlziptemporary-files

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.


Solution

  • 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' )
    );