Best way to download a file in PHP

后端 未结 3 2003
栀梦
栀梦 2020-12-10 09:26

Which would be the best way to download a file from another domain in PHP? i.e. A zip file.

相关标签:
3条回答
  • 2020-12-10 09:49

    normally, the fopen functions work for remote files too, so you could do the following to circumvent the memory limit (but it's slower than file_get_contents)

    <?php
    $remote = fopen("http://www.example.com/file.zip", "rb");
    $local = fopen("local_name_of_file.zip", 'w');
    while (!feof($remote)) {
      $content = fread($remote, 8192);
      fwrite($local, $content);
    }
    fclose($local);
    fclose($remote);
    ?>
    

    copied from here: http://www.php.net/fread

    0 讨论(0)
  • 2020-12-10 09:53

    The easiest one is file_get_contents(), a more advanced way would be with cURL for example. You can store the data to your harddrive with file_put_contents().

    0 讨论(0)
  • 2020-12-10 10:04

    You may use one code line to do this:

    copy(URL, destination);
    

    This function returns TRUE on success and FALSE on failure.

    0 讨论(0)
提交回复
热议问题