【发布时间】:2017-06-01 09:03:59
【问题描述】:
有些日子我从 URL 下载时遇到问题。检查了很多方法,但我无法下载超过 123MB 的 URL 文件!
我的代码:
.htaccess
<IfModule php5_module>
php_value allow_url_fopen On
php_flag asp_tags Off
php_flag display_errors Off
php_value max_execution_time 10000
php_value max_input_time 10000
php_value max_input_vars 10000
php_value memory_limit 12800M
php_value upload_max_filesize 2000M
</IfModule>
functions.php
<?php
ini_set('memory_limit', '-1');
ini_set('upload_max_size' , '1024M' );
ini_set('post_max_size', '1024M');
ini_set('max_execution_time', '10000' );
set_time_limit (24 * 60 * 60);
//---------------------------------------------------------
function download_url_one($url , $filename)
{
//first way
file_put_contents($filename, fopen($url, 'r'));
}
//---------------------------------------------------------
function download_url_two($url , $filename , $post = null)
{
//second way
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_AUTOREFERER, true);
curl_setopt($ch, CURLOPT_REFERER, "https://www.youtube.com/");
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT , 200000);
curl_setopt($ch, 156 , 200000);//CURLOPT_CONNECTTIMEOUT_MS
curl_setopt($ch, CURLOPT_TIMEOUT, 200000); //timeout in seconds
curl_setopt($ch, 155, 200000);//CURLOPT_TIMEOUT_MS
$result = curl_exec($ch);
curl_close($ch);
file_put_contents($filename, $result);
}
//---------------------------------------------------------
function download_url_three($url , $filename)
{
//third way
exec("wget $url -O $filename");
}
//---------------------------------------------------------
function download_url_four($url , $filename)
{
//fourth way
file_put_contents($filename, file_get_contents($url));
}
//---------------------------------------------------------
?>
index.php
<?php
include_once('functions.php');
$url = 'https://pc.tedcdn.com/talk/podcast/2004/None/DanGilbert_2004-480p.mp4';
download_url_one($url , 'example.mp4');
//download_url_two($url , 'example.mp4');
//download_url_three($url , 'example.mp4');
//download_url_four($url , 'example.mp4');
?>
此网址文件大小为143.76 MB。使用下载网址的每个功能后,只完成了大约 123 MB 的下载。然后显示错误500 Internal Server Error. Request Timeout. This request takes too long to process, it is timed out by the server. If it should not be timed out, please contact administrator of this web site to increase 'Connection Timeout'.
例如:
Function download_url_one(已下载 128.02 MB)Function download_url_two->(已下载 122.54 MB)-
Function download_url_three->(已下载 128.02 MB) -
Function download_url_four->(已下载 124.45 MB)
我与我的服务器支持人员进行了交谈,并将相同的设置应用于此图片。
请帮帮我,如何解决这个问题?
【问题讨论】:
-
脚本运行多长时间?您已将
time_limit设置为 1 小时。下载120mb需要那么长时间吗? -
感谢@Halcyon。脚本运行大约 2 分钟。如何设置
time_limit?在php上设置这个?我在第一行设置了一些functions.php... . -
您能确定导致超时的原因吗?您的服务器、删除服务器或中间的某个代理?
-
@Halcyon 我使用 cpanel 共享主机...。我现在检查此代码以查看主机中的时间限制。
<?php sleep(220); echo "hi"; ?>但在 220 秒后显示500 Internal Server Error。我设置了超过 24 小时的时间限制! -
我明白了。以我的经验,共享主机可以以任意数量的奇异方式进行配置。无论您的配置如何,都可能强制执行 220 秒限制。如果能够处理大文件对您来说真的很重要,请考虑切换到您可以控制的服务器(即没有 cpanel 废话)。
标签: php curl exec fopen file-get-contents