PHP Force下载导致0字节文件

问题描述 投票:7回答:8

我正在尝试使用PHP从我的Web服务器强制下载文件。我不是PHP的专家,但我似乎无法解决大小为0字节的文件下载问题。

码:

$filename = "FILENAME...";

header("Content-type: $type");
header("Content-Disposition: attachment;filename=$filename");
header("Content-Transfer-Encoding: binary");
header('Pragma: no-cache');
header('Expires: 0');
set_time_limit(0);
readfile($file);

有人可以帮忙吗?谢谢。

php download
8个回答
6
投票

您没有检查该文件是否存在。试试这个:

$file = 'monkey.gif';

if (file_exists($file))
{
    header('Content-Description: File Transfer');
    header('Content-Type: application/octet-stream');
    header('Content-Disposition: attachment; filename='.basename($file));
    header('Content-Transfer-Encoding: binary');
    header('Expires: 0');
    header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
    header('Pragma: public');
    header('Content-Length: ' . filesize($file));
    ob_clean();
    flush();
    readfile($file);
    exit;
}else
{
    echo "File does not exists";
}

看看你得到了什么。


您还应该注意,这会强制下载为八位字节流,即纯二进制文件。有些浏览器很难理解文件的确切类型。例如,如果您发送带有Content-Type: application/octet-stream标题的GIF,则浏览器可能不会将其视为GIF图像。您应该添加特定的检查以确定文件的内容类型,并发送适当的Content-Type标头。


2
投票

您需要指定Content-Length标头:

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

此外,你不应该发送Content-Transfer-Encoding标头。 HTTP/1.0HTTP/1.1 specs state都表示“HTTP不使用RFC 1521的内容传输编码(CTE)字段”。


2
投票

我在phunction中使用以下方法,到目前为止我还没有遇到任何问题:

function Download($path, $speed = null)
{
    if (is_file($path) === true)
    {
        $file = @fopen($path, 'rb');
        $speed = (isset($speed) === true) ? round($speed * 1024) : 524288;

        if (is_resource($file) === true)
        {
            set_time_limit(0);
            ignore_user_abort(false);

            while (ob_get_level() > 0)
            {
                ob_end_clean();
            }

            header('Expires: 0');
            header('Pragma: public');
            header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
            header('Content-Type: application/octet-stream');
            header('Content-Length: ' . sprintf('%u', filesize($path)));
            header('Content-Disposition: attachment; filename="' . basename($path) . '"');
            header('Content-Transfer-Encoding: binary');

            while (feof($file) !== true)
            {
                echo fread($file, $speed);

                while (ob_get_level() > 0)
                {
                    ob_end_flush();
                }

                flush();
                sleep(1);
            }

            fclose($file);
        }

        exit();
    }

    return false;
}

您只需执行以下操作即可尝试:

Download('/path/to/file.ext');

1
投票

这个问题和我的网站项目一样。这个代码我用过:

<?php

$file = $_SERVER["DOCUMENT_ROOT"].'/.../.../'.$_GET['file'];

 if(!file)
 {
     // File doesn't exist, output error
     die('file not found');
 }
 else
 {

    //$file_extension = strtolower(substr(strrchr($file,"."),1));
    $file_extension = end(explode(".", $file));

    switch( $fileExtension)
    {
    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":
    case "jpg": $ctype="image/jpg"; break;
    default: $ctype="application/force-download";
    }

    nocache_headers();

     // Set headers
    header("Pragma: public"); // required
    header("Expires: 0");
    header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
    header("Cache-Control: public"); // required for certain browsers
    header("Content-Description: File Transfer");
    header("Content-Type: $ctype");
    header("Content-Disposition: attachment; filename=".$file.";" );
    header("Content-Transfer-Encoding: binary");
    header("Content-Length: ".filesize($file));

     readfile($file);
 }
 ?>

我认为问题出在服务器设置上,如PHP设置或缓存设置,但我不知道这样做我的意见。


1
投票

我这样做是为了下载PDF ...

$filename = 'Application.pdf';
header("Content-Type: application/pdf");
header("Content-Disposition: attachment; filename=$filename");
echo $pdf;

我想你错过了最后一行,你实际上发送了$file中你所拥有的文件内容。

巴勃罗


0
投票

当我将目录更改为文件位置时,文件打开正常。

$reportLocation = REPORTSLOCATION;

$curDir = getcwd();

chdir($reportLocation);

if (file_exists($filename)) {

    header('Content-Disposition: attachment; filename="' . $filename . '"');
    header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
    header('Content-Length: ' . filesize($filename));
    header('Content-Transfer-Encoding: binary');
    header('Cache-Control: must-revalidate');
    header('Pragma: public');

    ob_clean();
    flush();

    readfile($filename);
}
chdir($curDir);

0
投票

诀窍是检查file_exists以确认正确的路径。令人困惑的是,对于PHP路径,您不需要以'/'开头来表示您的网站开始路径。

 '/folder/file.ext'  #bad, needs to know the real full path

在php /是网站的主要路径,你不需要提及它。只是:

 'folder/file.ext'   #relative to the / website

这将适用于file_exists,头文件名,readfile等...


0
投票

如果脚本工作适用于小文件,但对于大文件返回0字节文件。你必须通过php.ini增加memory_limit,你也可以在你的代码之前添加以下行

ini_set('memory_limit','-1');
© www.soinside.com 2019 - 2024. All rights reserved.