下载文件使用fopen(),fread(), fof()…下载的文件损坏


Downloading files using fopen(),fread(),feof()... Downloaded files are broken

我有一个从服务器下载文件的脚本,所有工作都很好。但当文件被下载时,它要么是jpg文件,要么是png文件。我似乎无法用任何程序打开它来查看它。(windows相册或烟花)

下载视频文件(avi)时。我不能用windows媒体播放器打开它,但我可以用vlc媒体播放器打开它。

我总是得到一个错误消息,不能读取文件或文件损坏。

下面是代码,它是可靠的还是我应该考虑使用fsocketopen或curl。

我试着改变标题在网上寻找更多的答案,没有运气。

有谁知道怎么回事吗?

chmod("extensions_img/test.jpg",0755);
$fullPath = "http://www.website_url.com/extensions_img/test.jpg";
if ($fd = fopen ($fullPath, "r")) {
    $fsize = filesize("extensions_img/test.jpg");
    $path_parts = pathinfo($fullPath);
    $ext = strtolower($path_parts["extension"]);
    switch($ext) {
        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/mp3";
            break;
        case "wav":
            $ctype = "audio/x-wav";
            break;
        case "wma":
            $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";
    }
    header("Pragma: public");
    header("Expires: 0");
    header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
    header("Cache-Control: private",false);
    header("Content-type: " . $ctype);
    header("Content-Disposition: attachment; filename='"".$path_parts["basename"]."'"");
    header("Content-Transfer-Encoding: binary");
    header("Content-length: $fsize");
    header("Cache-control: public"); //use this to open files directly
    while(!feof($fd)) {
        $buffer = fread($fd, 4096);
    flush();
    }
}
fclose ($fd);

尝试:

while(!feof($fd)) {
    echo fread($fd, 4096);
    flush();
}

你没有回显任何东西,所以你的文件可能是空的?

您正在发送Content-length:标头,其中包含您获得的错误文件大小。

// You are opening a remote file:
$fullPath = "http://www.website_url.com/extensions_img/test.jpg";
// You are retrieving the filesize of a local file that may or may not exist
$fsize = filesize("extensions_img/test.jpg");

所以,最有可能的是,您的Content-length看起来像这样:

 header("Content-length: 0");

…除非您有一个实际的同名本地文件,否则您将提供其文件大小,而不是预期的远程文件。

根据filesize()文档,支持一些URL包装器。您可以尝试从远程文件中检索文件大小:

从PHP 5.0.0开始,这个函数也可以与一些URL包装器一起使用。参考支持的协议和包装器来确定
哪些包装器支持stat()系列功能。

$fsize = filesize($fullpath);

使用filesize($url)将下载文件两次。当有file_get_contentsfpassthru时,不知道为什么要使用fopen/fread:

$data = file_get_contents($url);
$size = strlen($data);
$mime = current(preg_grep('/^Content-Type:/i', $http_response_header));

请注意,这也为您提供了正确的MIME类型(只是通过header简单地传递整个字符串)。