doutang0335 2011-07-17 13:48
浏览 85

PHP标头附加AVI文件

I'm trying to make a PHP script to download an AVI-file. The file is on my server and I want to send it to the user. I have made the following script but when I run it, I will only get a 0 KB large AVI file.

Can anyone tell me what I am doing wrong?

$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);

This is what I get from LiveHTTPHeaders in Firefox (for some reason Content-Lengthis zero):

HTTP/1.1 200 OK
Date: Sun, 17 Jul 2011 14:34:24 GMT
Server: Apache/2.2.6 mod_auth_kerb/5.3 PHP/5.2.17 mod_fcgid/2.3.5
X-Powered-By: PHP/5.2.17
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Pragma: no-cache
Content-Disposition: attachment; filename="test.avi"
Content-Length: 0
Connection: close
Content-Type: video/x-msvideo

You may want to try the site out yourself at http://snuzzer.dk/nas/client.php

  • 写回答

2条回答 默认 最新

  • doushiposong30622 2011-07-17 13:56
    关注

    Add this line before readfile():

     header("Content-Length: " . filesize($file_path));
    
    评论

报告相同问题?

悬赏问题

  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大