下载的视频文件零字节

时间:2017-11-09 00:02:15

标签: php apache

当我下载视频文件时,我得到零字节文件大小。以下是视频下载的代码。需要一些帮助

// Check if file exist
if (!file_exists($path)) {
     header('HTTP/1.1 404 Not Found');
     exit();
}

// Determine file mimetype

$finfo = new finfo(FILEINFO_MIME);
$mime = $finfo->file($path);

// File size

$size = filesize($path);

// Check if we have a Range header
if (isset($_SERVER['HTTP_RANGE'])) {
    // Parse field value
    list($specifier, $value) = explode('=', $_SERVER['HTTP_RANGE']);

    // Can only handle bytes range specifier

    if ($specifier != 'bytes') {
        header('HTTP/1.1 400 Bad Request');
        return;
    }

    // Set start/finish bytes

    list($from, $to) = explode('-', $value);
    if (!$to) {
        $to = $size;
    }

    // Response header

    header('HTTP/1.1 206 Partial Content');
    header('Accept-Ranges: bytes');

    // Response size
    header('Content-Length: ' . ($to - $from));

    // Range being sent in the response
    header("Content-Range: bytes ".$from."-".($to-1)."/{$size}");

    // Open file in binary mode
    $fp = fopen($path, 'rb');

    // Advance to start byte
    fseek($fp, $from);

    // Send the data
    while(true){
        // Check if all bytes have been sent
        if(ftell($fp) >= $to){
            break;
        }

        // Send data
        echo fread($fp, 8192); 
        // Flush buffer
        flush();
        ob_flush();
    }
} else {

    // If no Range header specified, send everything
    header('Content-Length: ' . $size);

    // Send file to client
    readfile($path);
}
} else {
    $path = "$media_dir/$filename";
    header("Content-Type: video/mp4");
}       
}

0 个答案:

没有答案