PHP 标头附加 AVI 文件 [英] PHP header attach AVI-file

查看:46
本文介绍了PHP 标头附加 AVI 文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试制作一个 PHP 脚本来下载 AVI 文件.该文件在我的服务器上,我想将其发送给用户.我已经制作了以下脚本,但是当我运行它时,我只会得到一个 0 KB 的大 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);

这是我从 Firefox 中的 LiveHTTPHeaders 得到的(出于某种原因 Content-Length 为零):

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

您可能想在 http://snuzzer.dk/nas/client 上亲自试用该网站.php

推荐答案

readfile()前添加这一行:

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

这篇关于PHP 标头附加 AVI 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆