PHP实现文件断点续传下载

PHP实现文件断点续传下载

<?php
//PHP 断点续传下载
function download($fileurl, $start = 0, $end = '')
{
     $task_sourcefile = 'downfile.mp4'; //本地保存的文件
        $sourceurl = $fileurl ;  //要下载的远程文件
        $tokenfile =  'downfile.token';
    if (!is_dir(dirname($tokenfile))) {
         mkdir(dirname($tokenfile), 0777, true);
    }
        
     $ch = curl_init($sourceurl);
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
     curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
     curl_setopt($ch, CURLOPT_HEADER, 0);
     curl_setopt($ch, CURLOPT_USERAGENT, 'SyncTask');

     curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 3600);
     curl_setopt($ch, CURLOPT_TIMEOUT, 3600);
     curl_setopt($ch, CURLOPT_BUFFERSIZE, 20971520);
    if ($end) {
         curl_setopt($ch, CURLOPT_RANGE, "$start-$end");
    } else {
         curl_setopt($ch, CURLOPT_RANGE, "$start-");
    }
        
     $flag=$tag = 0;

     curl_setopt($ch, CURLOPT_WRITEFUNCTION, function ($ch, $str) use (&$flag, $task_sourcefile, $end, $sourceurl, $tokenfile, $start, &$tag) {
         $len = strlen($str);
         $flag++;
        if ($flag==1) {
             $httpcode  = curl_getinfo($ch, CURLINFO_HTTP_CODE);
             $length  = curl_getinfo($ch, CURLINFO_CONTENT_LENGTH_DOWNLOAD);
            if ($length ==-1) {
                 $tag = 2;
                 return false;
            }
            if ($httpcode!=200 && $httpcode!=206) {
                 $tag = 2;
                 return false;
            }
            if ($start ==0) {
                 $data= ['name'=>$sourceurl,'size'=>$length];
                 file_put_contents($tokenfile, json_encode($data, JSON_UNESCAPED_UNICODE));
            }
            if ($end) {
                if ($end-$start!=$length) {
                     $tag = 1;
                     return false;
                }
            }
        }
         file_put_contents($task_sourcefile, $str, FILE_APPEND|LOCK_EX);
         return $len;
     });
     $output = curl_exec($ch);
    if ($tag ==2 || $output===false) {
         curl_close($ch);
         throw new Exception('下载出错:视频源文件无法访问');
    }
    if ($tag ==1) {
         unlink($task_sourcefile);
         unlink($tokenfile);
         curl_close($ch);
         download($fileurl, 0, '');
         return true;
    }
         
     $tokeninfo  = json_decode(file_get_contents($tokenfile), true);
     $length = $tokeninfo['size'];
     sleep(1);
     clearstatcache();//这里要去除文件信息缓存
    if ($length!= filesize($task_sourcefile)) {
         curl_close($ch);
         throw new Exception('下载出错:长度不长度不匹配');
    }
     curl_close($ch);
     return true;
}
 
    //run
   $download= download('http://www.test.com/333.mp4');
   var_dump($download);

 

原创文章,转载请注明来自Lenix的博客,地址 http://blog.p2hp.com/archives/5441

 

PHP实现文件断点续传下载
标签:     

发表评论

电子邮件地址不会被公开。 必填项已用*标注

*