private function downloadFile($url, $path)
{
$newfname = $path;
$file = fopen ($url, 'rb');
if ($file) {
$newf = fopen ($newfname, 'wb');
if ($newf) {
while(!feof($file)) {
fwrite($newf, fread($file, 1024 * 8), 1024 * 8);
}
}
}
if ($file) {
fclose($file);
}
if ($newf) {
fclose($newf);
}
}
7
尝试使用cURL
set_time_limit(0); // unlimited max execution time
$options = array(
CURLOPT_FILE => '/path/to/download/the/file/to.zip',
CURLOPT_TIMEOUT => 28800, // set this to 8 hours so we dont timeout on big files
CURLOPT_URL => 'http://remoteserver.com/path/to/big/file.zip',
);
$ch = curl_init();
curl_setopt_array($ch, $options);
curl_exec($ch);
curl_close($ch);
我不确定,但我相信它会在拉动数据时写入 CURLOPT_FILE 选项,即 . 不缓冲 .
4
在目标服务器中创建名为"downloads"的文件夹
将[此代码]保存到 .php 文件中并在目标服务器中运行
下载器:
<html>
<form method="post">
<input name="url" size="50" />
<input name="submit" type="submit" />
</form>
<?php
// maximum execution time in seconds
set_time_limit (24 * 60 * 60);
if (!isset($_POST['submit'])) die();
// folder to save downloaded files to. must end with slash
$destination_folder = 'downloads/';
$url = $_POST['url'];
$newfname = $destination_folder . basename($url);
$file = fopen ($url, "rb");
if ($file) {
$newf = fopen ($newfname, "wb");
if ($newf)
while(!feof($file)) {
fwrite($newf, fread($file, 1024 * 8 ), 1024 * 8 );
}
}
if ($file) {
fclose($file);
}
if ($newf) {
fclose($newf);
}
?>
</html>
10 回答
从PHP 5.1.0开始,file_put_contents()支持通过传递一个流句柄作为
$data
参数逐个编写:从手册:
(谢谢Hakre . )
尝试使用cURL
我不确定,但我相信它会在拉动数据时写入
CURLOPT_FILE
选项,即 . 不缓冲 .在目标服务器中创建名为"downloads"的文件夹
将[此代码]保存到
.php
文件中并在目标服务器中运行下载器:
上面有代码(由prodigitalson引用)代码wchih不起作用(原因:在CURLOPT_FILE中缺少fopen - http://www.webdeveloper.com/forum/showthread.php?268299-RESOLVED-PHP-script-for-a-cronjob-download-file-unpzck-run-another-php-script) . 我不能在那里添加评论因为我的点数太少所以下面我给出了工作示例(它也适用于"local url"):
有3种方式:
file_get_contents和file_put_contents
CURL
fopen
你可以找到例子from here .
我用它来下载文件
A PHP 4 & 5 Solution:
即使在发送大文件时, readfile() 也不会出现任何内存问题 . 如果已启用fopen包装器,则URL可用作此函数的文件名 .
http://php.net/manual/en/function.readfile.php
在php中使用一个简单的方法
copy()
注意:如果目标文件已存在,则将覆盖该文件
PHP copy() Function
特别注意:不要忘记为目标文件夹设置权限777