php恢复idm的容量

时间:2014-03-04 08:15:40

标签: php

我的代码在php文件中用于下载任何文件,但恢复功能显示未知的plz你可以建议我该功能的代码

我的php页面下载文件,必须更改代码或如何更改idm中的恢复功能?                  

       if(!isset($_SESSION['user_id'])){
       header('location:../index.php');}
       $uname=$_SESSION['uname'];
       $uid= $_SESSION['user_id'];
     function output_file($file, $name, $mime_type='')
     {
/*
This function takes a path to a file to output ($file),  the filename that the browser will see ($name) and  the MIME type of the file ($mime_type, optional).
*/

//Check the file premission
//if(!is_readable($file)) die('File not found or inaccessible!');

$size = filesize($file);
$name = rawurldecode($name);

/* Figure out the MIME type | Check in array */
$known_mime_types=array(
    "pdf" => "application/pdf",
    "txt" => "text/plain",
    "html" => "text/html",
    "htm" => "text/html",
    "exe" => "application/octet-stream",
    "zip" => "application/zip",
    "doc" => "application/msword",
    "xls" => "application/vnd.ms-excel",
    "ppt" => "application/vnd.ms-powerpoint",
    "gif" => "image/gif",
    "png" => "image/png",
    "jpeg"=> "image/jpg",
    "jpg" =>  "image/jpg",
    "php" => "text/plain"
);

if($mime_type==''){
    $file_extension = strtolower(substr(strrchr($file,"."),1));
    if(array_key_exists($file_extension, $known_mime_types)){
        $mime_type=$known_mime_types[$file_extension];
    } else {
        $mime_type="application/force-download";
    };
};

//turn off output buffering to decrease cpu usage
@ob_end_clean(); 

// required for IE, otherwise Content-Disposition may be ignored
if(ini_get('zlib.output_compression'))
ini_set('zlib.output_compression', 'Off');

header('Content-Type: ' . $mime_type);
header('Content-Disposition: attachment; filename="'.$name.'"');
header("Content-Transfer-Encoding: binary");
header('Accept-Ranges: bytes');
header('Content-Length: 2052595');
header('Content-Range: bytes 339843-2392437/2392438');

/* The three lines below basically make the 
    download non-cacheable */
header("Cache-control: private");
header('Pragma: private');
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");

// multipart-download and download resuming support
if(isset($_SERVER['HTTP_RANGE']))
{
    list($a, $range) = explode("=",$_SERVER['HTTP_RANGE'],2);
    list($range) = explode(",",$range,2);
    list($range, $range_end) = explode("-", $range);
    $range=intval($range);
    if(!$range_end) {
        $range_end=$size-1;
    } else {
        $range_end=intval($range_end);
 }

$new_length = $range_end-$range+1;
header("HTTP/1.1 206 Partial Content");
header("Content-Length: $new_length");
header("Content-Range: bytes $range-$range_end/$size");
} else {
    $new_length=$size;
    header("Content-Length: ".$size);
}

/* Will output the file itself */
$chunksize = 3*(1024*1024); //you may want to change this
$bytes_send = 0;
if ($file = fopen($file, 'r'))
{
    if(isset($_SERVER['HTTP_RANGE']))
    fseek($file, $range);

while(!feof($file) && 
(!connection_aborted()) && 
    ($bytes_send<$new_length)
      )
{
$buffer = fread($file, $chunksize);
    print($buffer); //echo($buffer); // can also possible
    flush();
    $bytes_send += strlen($buffer);
}
fclose($file);
} else
//If no permissiion
die('Error - can not open file.');
//die
die();
}
//Set the time out
set_time_limit(0);
$temp = explode("\\", $_REQUEST['filename']);

//path to the file
$file_path=$_REQUEST['filename'];


//Call the download function with file path,file name and file type
output_file($file_path, ''.$temp[3].'', 'text/plain');


     ?>

2 个答案:

答案 0 :(得分:4)

<?php
$uname=$_COOKIE["uname"];
//echo $uname;
$file=$_REQUEST['filename'];
$file_name="E:\\upload\\$uname\\$file";
$fid=$_REQUEST['fid'];


$con=mysql_connect("localhost","root","Net@123");
$db=mysql_select_db("file_storage",$con);
$query1=mysql_query("SELECT * FROM upload WHERE file_id=$fid");
$row1=mysql_fetch_array($query1);

    if($row1)
    {
        $file_db=$row1['name'];

// If the requested file is exist
    if(file_exists($file_name)){
            // Get the file size
            $file_size=filesize($file_name);
            // Open the file
            $fh=fopen($file_name, "r");


// Download speed in KB/s
            $speed=1024;


//Initialize the range of bytes to be transferred
            $start=0;
            $end=$file_size-1;


//Check HTTP_RANGE variable
            if(isset($_SERVER['HTTP_RANGE']) &&
                    preg_match('/^bytes=(\d+)-(\d*)/', $_SERVER['HTTP_RANGE'], $arr)){

                    // Starting byte
                    $start=$arr[1];
                    if($arr[2]){
                            // Ending byte
                            $end=$arr[2];
                    }
            }


//Check if starting and ending byte is valid
            if($start>$end || $start>=$file_size){
                    header("HTTP/1.1 416 Requested Range Not Satisfiable");
                    header("Content-Length: 0");
            }
            else{
                    // For the first time download
                    if($start==0 && $end==$file_size){
                            // Send HTTP OK header
                            header("HTTP/1.1 200 OK");
                    }
                    else{
                            // For resume download
                            // Send Partial Content header
                            header("HTTP/1.1 206 Partial Content");
                            // Send Content-Range header
                   header("Content-Range: bytes ".$start."-".$end."/".$file_size);
                    }
                    // Bytes left
                    $left=$end-$start+1;


                    // Send the other headers
                    header("Content-Type: application/octet-stream");
                    header("Accept-Ranges: bytes");
                    // Content length should be the bytes left
                    header("Content-Length: ".$left);
                    header("Content-Disposition: attachment; filename=".$file_db);

                    // Read file from the given starting bytes
                    fseek($fh, $start);
                    // Loop while there are bytes left
                    while($left>0){
                            // Bytes to be transferred
                            // according to the defined speed
                            $bytes=$speed*1024;
                            // Read file per size
                            echo fread($fh, $bytes);
                            // Flush the content to client
                            flush();
                            // Substract bytes left with the tranferred bytes
                            $left-=$bytes;
                            // Delay for 1 second
                            sleep(1);
                    }
            }


    fclose($fh);
    }
    else{
            //If the requested file is not exist
            //Display error message
            echo "File not found!";
    }
    }
    exit();

?>

此代码适用于,文件正在下载并具有恢复功能...

答案 1 :(得分:1)

请参阅有关如何向客户报告功能的标题上的RFC:

http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html

14.5接受范围

Accept-Ranges response-header字段允许服务器 表明它接受资源的范围请求:

      Accept-Ranges     = "Accept-Ranges" ":" acceptable-ranges
      acceptable-ranges = 1#range-unit | "none"

接受字节范围请求的原始服务器可以发送

      Accept-Ranges: bytes

但不要求这样做。客户端可以生成字节范围 请求没有收到资源的此标头 参与其中。范围单位在3.12节中定义。

不接受任何类型的范围请求的服务器 资源可以发送

      Accept-Ranges: none

建议客户不要尝试范围请求。