PHP实现下载文件的函数以及设置脚本的执行时间
怎么设置脚本执行时间
设置方法:
1、php.ini中,查找并设置“max_execution_time”项的值;
max_execution_time = 120;
2、通过“ini_set("max_execution_time","数值");”语句设置;
ini_set("max_execution_time", "120");
3、通过“set_time_limit(数值)”语句设置。
set_time_limit(0);
以上几个数字设置为0则无限制,脚本会一直执行下去,直到执行结束。
使用curl实现动态下载
<?php
echo httpcopy("http://www.baidu.com/img/baidu_sylogo1.gif");
function httpcopy($url, $file="", $timeout=60) {
$file = empty($file) ? pathinfo($url,PATHINFO_BASENAME) : $file;
$dir = pathinfo($file,PATHINFO_DIRNAME);
!is_dir($dir) && @mkdir($dir,0755,true);
$url = str_replace(" ","%20",$url);
if(function_exists('curl_init')) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$temp = curl_exec($ch);
if(@file_put_contents($file, $temp) && !curl_error($ch)) {
return $file;
} else {
return false;
}
} else {
$opts = array(
"http"=>array(
"method"=>"GET",
"header"=>"",
"timeout"=>$timeout)
);
$context = stream_context_create($opts);
if(@copy($url, $file, $context)) {
//$http_response_header
return $file;
} else {
return false;
}
}
}
?>
函数参数说明:
$url:要下载的文件的链接
$file: 下载文件存放的地址
$timeout:请求超时的时间
比如:
$src = 'http://www.wubin.work/xxx.mp4';
$path = str_replace('\\', '/', __DIR__);
mp4Name = basename($src);
$mp4SavePath = "{$path}/mp4/{$mp4Name}";
$res = httpcopy($src, $mp4SavePath);
if(!$res) {...}
再来个远程下载文件到服务器
<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 = 'temp/';
$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);
}
?>
使用header下载
<?php
$url = 'xxx';
$file = file_get_contents($url);
header("Content-Disposition:attachment;filename=1.flv");
echo $file;
?>
或者
<?php
$url = ‘xxx’
header("Content-type:video/mpeg4");
header("Content-Disposition:attachment;filename=1.flv");
readfile($url);
exit;
?>
然后成功下载1.flv文件,但是下载后不可以播放,使用linux直接wget http://a.com/1.flv下载的就可以播放。通过PHP直接下载的打开就会出现错误,对比文件大小发现不可以播放的多了3个字节.
解决方法:
这三个字符,是传说中的BOM啊
说明你用于下载的php文件,格式为UTF-8 with BOM,用Notepad++看一下呢,
或者把你所有的php文件讀出來,然后移除一下?
/**
* 移除字符串的BOM
*
* @param string $str 输入字符串
* @return string 输出字符串
*/
function removeBOM($str)
{
$str_2 = substr($str, 0, 2);
$str_3 = substr($str, 0, 3);//$str_2.$str{2};
$str_4 = substr($str, 0, 4);//$str_3.$str{3};
if ($str_3 == pack('CCC',0xef,0xbb,0xbf)) //utf-8
return substr($str, 3);
elseif ($str_2 == pack('CC',0xfe,0xff) || $str_2 == pack('CC',0xff,0xfe)) //unicode
return substr($str, 2);
elseif ($str_4 == pack('CCCC',0x00,0x00,0xfe,0xff) || $str_4 == pack('CCCC',0xff,0xfe,0x00,0x00)) //utf-32
return substr($str, 4);
return $str;
}