用PHP下载文件的最佳方式

时间:2022-04-24 18:11:00

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

哪个是从PHP下载另一个域的文件的最佳方法?即一个zip文件。

2 个解决方案

#1


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

最简单的是file_get_contents(),例如,更高级的方法是使用cURL。您可以使用file_put_contents()将数据存储到硬盘驱动器中。

#2


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

#1


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

最简单的是file_get_contents(),例如,更高级的方法是使用cURL。您可以使用file_put_contents()将数据存储到硬盘驱动器中。

#2


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