I have 2 files that I need to get on my server one is 600 megs the other is 1.4 gigs and the files are on a http site not ftp. How can i get php to download and save the files without me passing them?

if this were ftp I know how to do it but the http is giving me a problem with file_get_contents

this is what I have and isn't working:
//----------------------------------------------------------------------
set_time_limit(1200);
$url = "http://images.shopdbl.com/zips/medium.zip";
$zip_file = file_get_contents($url);
$ourFileHandle = fopen($ourFileName, 'w') or die("can't open file");
fwrite($ourFileHandle, $zip_file);
fclose($ourFileHandle);
//-----------------------------------------------------------------------

I'm not getting any errors and its stopping before the time set.

    if you dont get any error reports
    you maybe not have PHP set to it:
    error_reporting( E_ALL );

    as this is a fileupload action, where it start okay
    there are 2 things that may be issue:
    - time
    - size

    And it is well told about, here:
    Chapter 38. Handling file uploads
    http://php.net/file-upload

    🙂

      This is NOT a file upload. Its a download. The problem with the current code is that it will eat memory like hell. file_get_contents gets the file to a string which means that you would have to have 1,4Gb(probably even more) of memory reserved for 1,4Gb file.

      Use stream functions to read and copy the file. Check the manual:
      http://www.php.net/manual/en/function.stream-copy-to-stream.php

      In the first comment(by sundance2001) theres a working function to download the file. Modify that to your needs.

      That function needs PHP5. If you have only PHP4 in use, you can use fgets to retrieve/write the file piece by piece. Check the examples and comments also on that function.

      If I remember right, max_execution_time doesnt affect these functions. BTW, I tried to get the file (medium.zip) but the server timed out after ~45Mb. I didnt have any problems downloading large file from another server..

      If those doesnt work, you could also use fetch or wget(depends if they are installed and you can execute shell commands from php).

        cahva wrote:

        If those doesnt work, you could also use fetch or wget(depends if they are installed and you can execute shell commands from php).

        This is what I was going to suggest - hook wget or some other application to do it instead, because it's just going to be a pain in the ass doing it through PHP.

          Write a Reply...