PHP头附加AVI文件

PHP头附加AVI文件,php,download,content-type,attachment,avi,Php,Download,Content Type,Attachment,Avi,我正在尝试制作一个PHP脚本来下载AVI文件。该文件位于我的服务器上,我想将其发送给用户。我制作了以下脚本,但当我运行它时,我只会得到一个0 KB的大AVI文件 谁能告诉我我做错了什么 $file_path = "downloads/test.avi"; // Get filename $filename = explode("/", $file_path); $filename = $filename[count($filename)-1]; if(file_exists($file_pa

我正在尝试制作一个PHP脚本来下载AVI文件。该文件位于我的服务器上,我想将其发送给用户。我制作了以下脚本,但当我运行它时,我只会得到一个0 KB的大AVI文件

谁能告诉我我做错了什么

$file_path = "downloads/test.avi";

// Get filename
$filename = explode("/", $file_path);
$filename = $filename[count($filename)-1];

if(file_exists($file_path)) {
    $file_extension = strtolower(substr(strrchr($file_path, "."), 1));

    // This will set the Content-Type to the appropriate setting for the file
    switch($file_extension) {
        case "pdf":
            $ctype = "application/pdf";
            break;
        case "exe":
            $ctype = "application/octet-stream";
            break;
        case "zip":
            $ctype = "application/zip";
            break;
        case "doc":
            $ctype = "application/msword";
            break;
        case "xls":
            $ctype = "application/vnd.ms-excel";
            break;
        case "ppt":
            $ctype = "application/vnd.ms-powerpoint";
            break;
        case "gif":
            $ctype = "image/gif";
            break;
        case "png":
            $ctype = "image/png";
            break;
        case "jpeg":
            $ctype = "image/jpg";
            break;
        case "jpg":
            $ctype = "image/jpg";
            break;
        case "mp3":
            $ctype = "audio/mpeg";
            break;
        case "wav":
            $ctype = "audio/x-wav";
            break;
        case "mpeg":
            $ctype = "video/mpeg";
            break;
        case "mpg":
            $ctype = "video/mpeg";
            break;
        case "mpe":
            $ctype = "video/mpeg";
            break;
        case "mov":
            $ctype = "video/quicktime";
            break;
        case "avi":
            $ctype = "video/x-msvideo";
            break;
        case "src":
            $ctype = "plain/text";
            break;
        default:
            $ctype = "application/force-download";
    }

    $filesize = filesize($file_path);

    // Set content type
    header("Content-type: " . $ctype);

    // Download file
    header("Content-Disposition: attachment; filename=\"" . $filename . "\"");

    // Set size of file
    header("Content-Length: " . $filesize);

    readfile($file_path);
这是我从Firefox中的LiveHTTPHeaders中得到的(出于某种原因,
Content Length
为零):


您可能希望自己在

上尝试该站点,在readfile()之前添加这一行:


测试一下,我想这会有用的

header ( "Pragma: public" );
header ( "Expires: 0" );
header ( "Cache-Control: must-revalidate, post-check=0, pre-check=0" );
header ( "Content-Type: application/force-download" );
header ( "Content-Type: application/octet-stream" );
header ( "Content-Type: application/download" );
header ( "Content-Disposition: attachment; filename=\"" . $file_path . "\";" );
header ( "Content-Transfer-Encoding: binary " );
header ( "Content-type: " . $ctype);

$chunksize = 1 * (1024 * 1024); // how many bytes per chunk 
if ($size > $chunksize) { 
  $handle = fopen($file_path, 'rb'); 
  $buffer = ''; 
  while (!feof($handle)) { 
    $buffer = fread($handle, $chunksize); 
    echo $buffer; 
    ob_flush(); 
    flush(); 
  } 
  fclose($handle); 
  exit();

正如我刚刚写给@alex的一样,无论是否使用
Content Length
,结果都完全相同。我刚刚尝试在Firefox中使用LiveHTTPHeaders,由于某种原因,当我在其中使用
readfile()
时,Firefox不会打开页面,LiveHTTPHeaders说
Content Length
为0。请查看我的原始帖子。注意:使用一个数组,其中键作为文件扩展名,值作为mime类型。将减少您的代码并且可能更容易编辑ReadFile()返回读取的字节数,您能检查它是否真的读取了文件吗?可能是权限问题。
 header("Content-Length: " . filesize($file_path));
header ( "Pragma: public" );
header ( "Expires: 0" );
header ( "Cache-Control: must-revalidate, post-check=0, pre-check=0" );
header ( "Content-Type: application/force-download" );
header ( "Content-Type: application/octet-stream" );
header ( "Content-Type: application/download" );
header ( "Content-Disposition: attachment; filename=\"" . $file_path . "\";" );
header ( "Content-Transfer-Encoding: binary " );
header ( "Content-type: " . $ctype);

$chunksize = 1 * (1024 * 1024); // how many bytes per chunk 
if ($size > $chunksize) { 
  $handle = fopen($file_path, 'rb'); 
  $buffer = ''; 
  while (!feof($handle)) { 
    $buffer = fread($handle, $chunksize); 
    echo $buffer; 
    ob_flush(); 
    flush(); 
  } 
  fclose($handle); 
  exit();