简体   繁体   中英

Best way to download a file in PHP

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

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() .

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

You may use one code line to do this:

copy(URL, destination);

This function returns TRUE on success and FALSE on failure.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM