Home > php教程 > php手册 > body text

php断点续传

WBOY
Release: 2016-06-06 19:45:48
Original
808 people have browsed it

$file_dir = '/demo/'; $filename = 'text.txt'; $sourceFile = $file_dir.$filename; $fp = fopen($sourceFile, "rb"); //检测文件是否存在 if (!is_file($sourceFile)) { die("b404 File not found!/b"); } $filename = basename($sourceFile); //获取文件

   $file_dir = '/demo/';   
        $filename = 'text.txt';
        $sourceFile = $file_dir.$filename;
        $fp = fopen($sourceFile, "rb");
        //检测文件是否存在  
        if (!is_file($sourceFile)) {  
            die("404 File not found!");  
        } 
        $filename = basename($sourceFile); //获取文件名字  
        $file_size = filesize($sourceFile);  
        $size2 = $file_size -1; //文件总字节数

        $ctype = "application/vnd.android.package-archive";
        header("Cache-Control:");  
        header("Cache-Control: public");  
          
        //设置输出浏览器格式  
        header("Content-Type: $ctype");  
        header("Content-Disposition: attachment; filename=" . $filename);  
        header("Accept-Ranges: bytes");  

        if (isset($_SERVER ['HTTP_RANGE'] )) {
            if (preg_match ( '/bytes=\h*(\d+)-(\d*)[\D.*]?/i', $_SERVER ['HTTP_RANGE'], $matches )) {
                // 读取文件,起始节点
                    $begin = intval ( $matches [1] );
                // 读取文件,结束节点
                if (! empty ( $matches [2] )) {
                    $end = intval ( $matches [2] );
                }
            }
            $new_length = $file_size - $begin; //获取下次下载的长度  
            header("HTTP/1.1 206 Partial Content");  
               header("Content-Length: $new_length"); //输入总长  
               header("Content-Range: bytes $begin-$size2/$file_size" );
        }else{  
                header("Content-Range: bytes 0-$size2/$file_size"); //Content-Range: bytes 0-4988927/4988928  
                header("Content-Length: " . $file_size); //输出总长    
            }  

        $buffer=1024;  
        fseek($fp, $begin);  
        $file_count=0;  
        while(!feof($fp) && ($file_size-$file_count>0)){  
            $file_data=fread($fp,$buffer);
            $file_count+=$buffer;
            echo $file_data;
        }  
        fclose($fp);  
        exit ();

文件直接下载可以安装,断点那一块,文件下载完之后总是不能够安装,什么愿意,代码哪里错了?大神求解答。调试一天也没调出来,试了很多种方法。

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template