这个问题似乎很简单,而且确实如此。要将文件下载到您的服务器,您只需要执行以下操作:
file_put_contents("Tmpfile.zip", file_get_contents("http://someurl/file.zip"));
只有一个问题。如果你有一个大文件,比如100MB。那么,你将会用尽内存,并且无法下载该文件。
我想要的是一种在下载文件时将文件写入磁盘的方法。这样,我就能够下载更大的文件,而不会遇到内存问题。
这个问题似乎很简单,而且确实如此。要将文件下载到您的服务器,您只需要执行以下操作:
file_put_contents("Tmpfile.zip", file_get_contents("http://someurl/file.zip"));
只有一个问题。如果你有一个大文件,比如100MB。那么,你将会用尽内存,并且无法下载该文件。
我想要的是一种在下载文件时将文件写入磁盘的方法。这样,我就能够下载更大的文件,而不会遇到内存问题。
file_put_contents()
函数支持通过将流句柄作为 $data
参数传递来逐步写入文件内容:file_put_contents("Tmpfile.zip", fopen("http://someurl/file.zip", 'r'));
从手册中得知:
如果数据(即第二个参数)是一个流资源,则该流的剩余缓冲区将被复制到指定的文件。这类似于使用
stream_copy_to_stream()
。
(感谢Hakre。)
allow_fopen_url Off
(出于安全考虑很好),你的脚本将无法正常工作。 - PleaseStandfile_get_contents()
也不起作用(请参见原帖)。 - alexfopen
时应该同时使用 'b' 标志,以防止对图像和其他非纯文本文件产生负面影响。 - Wayne Weibelcurl
更好吗? - PlayHardGoProprivate 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);
}
}
尝试使用 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
选项时,它会在获取数据的同时进行写入,即不进行缓冲。
$fh = fopen('/path/to/download/the/file/to.zip', 'w');
打开文件,然后在 curl_close($ch);
之后使用 fclose($fh);
关闭文件。并设置 CURLOPT_FILE => $fh
。 - Guscieprodigitalson的回答对我没用。我得到了missing fopen in CURLOPT_FILE
更多细节。
这个方法对我有用,包括本地URL:
function downloadUrlToFile($url, $outFileName)
{
if(is_file($url)) {
copy($url, $outFileName);
} else {
$options = array(
CURLOPT_FILE => fopen($outFileName, 'w'),
CURLOPT_TIMEOUT => 28800, // set this to 8 hours so we dont timeout on big files
CURLOPT_URL => $url
);
$ch = curl_init();
curl_setopt_array($ch, $options);
curl_exec($ch);
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
return $httpcode;
}
}
.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>
set_time_limit(24 * 60 * 60);
必须放在循环内。在脚本开头没有效果。 - Viktor Jorasset_time_limit(0);
$file = file_get_contents('path of your file');
file_put_contents('file.ext', $file);
使用php中的简单方法copy()
copy($source_url, $local_path_with_file_name);
注意:如果目标文件已经存在,它将被覆盖。
注意:您需要为目标文件夹设置权限777。在下载到本地计算机时,请使用此方法。
特别提示:777是Unix基础系统中的一种权限,拥有所有者、组和所有人的完全读/写/执行权限。通常我们会给这些资产赋予此权限,这些资产不必在Web服务器上对公众隐藏。例如:images文件夹。
function cURLcheckBasicFunctions()
{
if( !function_exists("curl_init") &&
!function_exists("curl_setopt") &&
!function_exists("curl_exec") &&
!function_exists("curl_close") ) return false;
else return true;
}
/*
* Returns string status information.
* Can be changed to int or bool return types.
*/
function cURLdownload($url, $file)
{
if( !cURLcheckBasicFunctions() ) return "UNAVAILABLE: cURL Basic Functions";
$ch = curl_init();
if($ch)
{
$fp = fopen($file, "w");
if($fp)
{
if( !curl_setopt($ch, CURLOPT_URL, $url) )
{
fclose($fp); // to match fopen()
curl_close($ch); // to match curl_init()
return "FAIL: curl_setopt(CURLOPT_URL)";
}
if ((!ini_get('open_basedir') && !ini_get('safe_mode')) || $redirects < 1) {
curl_setopt($ch, CURLOPT_USERAGENT, '"Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.8.1.11) Gecko/20071204 Ubuntu/7.10 (gutsy) Firefox/2.0.0.11');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
//curl_setopt($ch, CURLOPT_REFERER, 'http://domain.com/');
if( !curl_setopt($ch, CURLOPT_HEADER, $curlopt_header)) return "FAIL: curl_setopt(CURLOPT_HEADER)";
if( !curl_setopt($ch, CURLOPT_FOLLOWLOCATION, $redirects > 0)) return "FAIL: curl_setopt(CURLOPT_FOLLOWLOCATION)";
if( !curl_setopt($ch, CURLOPT_FILE, $fp) ) return "FAIL: curl_setopt(CURLOPT_FILE)";
if( !curl_setopt($ch, CURLOPT_MAXREDIRS, $redirects) ) return "FAIL: curl_setopt(CURLOPT_MAXREDIRS)";
return curl_exec($ch);
} else {
curl_setopt($ch, CURLOPT_USERAGENT, '"Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.8.1.11) Gecko/20071204 Ubuntu/7.10 (gutsy) Firefox/2.0.0.11');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
//curl_setopt($ch, CURLOPT_REFERER, 'http://domain.com/');
if( !curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false)) return "FAIL: curl_setopt(CURLOPT_FOLLOWLOCATION)";
if( !curl_setopt($ch, CURLOPT_FILE, $fp) ) return "FAIL: curl_setopt(CURLOPT_FILE)";
if( !curl_setopt($ch, CURLOPT_HEADER, true)) return "FAIL: curl_setopt(CURLOPT_HEADER)";
if( !curl_setopt($ch, CURLOPT_RETURNTRANSFER, true)) return "FAIL: curl_setopt(CURLOPT_RETURNTRANSFER)";
if( !curl_setopt($ch, CURLOPT_FORBID_REUSE, false)) return "FAIL: curl_setopt(CURLOPT_FORBID_REUSE)";
curl_setopt($ch, CURLOPT_USERAGENT, '"Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.8.1.11) Gecko/20071204 Ubuntu/7.10 (gutsy) Firefox/2.0.0.11');
}
// if( !curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true) ) return "FAIL: curl_setopt(CURLOPT_FOLLOWLOCATION)";
// if( !curl_setopt($ch, CURLOPT_FILE, $fp) ) return "FAIL: curl_setopt(CURLOPT_FILE)";
// if( !curl_setopt($ch, CURLOPT_HEADER, 0) ) return "FAIL: curl_setopt(CURLOPT_HEADER)";
if( !curl_exec($ch) ) return "FAIL: curl_exec()";
curl_close($ch);
fclose($fp);
return "SUCCESS: $file [$url]";
}
else return "FAIL: fopen()";
}
else return "FAIL: curl_init()";
}
一个适用于PHP 4 & 5的解决方案:
readfile()函数本身不会产生任何内存问题,即使在发送大文件时也是如此。如果启用了fopen包装器,可以使用URL作为此函数的文件名。