简体   繁体   English

用PHP下载文件的最佳方式

[英]Best way to download a file in PHP

Which would be the best way to download a file from another domain in PHP? 哪个是从PHP中的另一个域下载文件的最佳方式? ie A zip file. 即一个zip文件。

The easiest one is file_get_contents() , a more advanced way would be with cURL for example. 最简单的是file_get_contents() ,例如,更高级的方法是使用cURL You can store the data to your harddrive with file_put_contents() . 您可以使用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) 通常,fopen函数也适用于远程文件,因此您可以执行以下操作来规避内存限制(但它比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 从这里复制: 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. 此函数在成功时返回TRUE,在失败时返回FALSE。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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