【问题标题】:PHP - simple download scriptPHP - 简单的下载脚本
【发布时间】:2012-08-31 00:13:08
【问题描述】:

我正在制作一个简单的下载脚本,我的用户可以在其中下载自己的图像等。

但我遇到了一些奇怪的问题。

当我下载文件时,无论我下载什么文件类型,它都包含我的 index.php 文件中的内容。我的代码是这样的:

$fullPath = $r['snptFilepath'] . $r['snptFilename'];

if (file_exists($fullPath)) {

    #echo $fullPath;

    // setting headers
    header('Content-Description: File Transfer');
    header('Cache-Control: public'); # needed for IE
    header('Content-Type: '.$r['snptFiletype'].'');
    header('Content-Disposition: attachment; filename='. $filename . '.' . $r['snptExtension']);
    header('Content-Length: '.$r['snptSize'].'');
    readfile($fullPath)or die('error!');
} else {
    die('File does not exist');
}

$r 是来自我的数据库的结果,上传文件时我在其中存储了大小、类型、路径等。

更新

当我上传和下载 *.pdf 文件时,它正在成功运行。但是当我尝试下载 *.zip 和 text/rtf、text/plain 时,它的行为很奇怪。

我的意思是奇怪:它下载完整的 index.php 文件,其中包含下载的文件内容。

回答

我从http://php.net/manual/en/function.readfile.php 复制了这个,现在可以使用了。似乎:ob_clean();成功了!谢谢大家的帮助。

#setting headers
    header('Content-Description: File Transfer');
    header('Content-Type: '.$type);
    header('Content-Disposition: attachment; filename='.basename($file));
    header('Content-Transfer-Encoding: binary');
    header('Expires: 0');
    header('Cache-Control: must-revalidate');
    header('Pragma: public');
    header('Content-Length: ' . filesize($file));
    ob_clean();
    flush();
    readfile($file);
    exit;

【问题讨论】:

  • 你从哪里得到 $filename?
  • 试试echo $fullpath;。是空字符串吗?
  • readfile("$fullPath") 此处无需使用"。尝试将header 和readfile 替换为echo,然后查看您实际发送到用户浏览器的内容。
  • @WayneWhitty $filename 来自 url。
  • @AlexanderLarikov 当我回显标题时,它看起来很好。当回显 readfile 时,我得到了很多内容,带有“?”-sign..

标签: php


【解决方案1】:

试试这个函数,或者在你的代码中实现这些头文件

    function force_download($filename) {
    $filedata = @file_get_contents($filename);

    // SUCCESS
    if ($filedata)
    {
        // GET A NAME FOR THE FILE
        $basename = basename($filename);

        // THESE HEADERS ARE USED ON ALL BROWSERS
        header("Content-Type: application-x/force-download");
        header("Content-Disposition: attachment; filename=$basename");
        header("Content-length: " . (string)(strlen($filedata)));
        header("Expires: ".gmdate("D, d M Y H:i:s", mktime(date("H")+2, date("i"), date("s"), date("m"), date("d"), date("Y")))." GMT");
        header("Last-Modified: ".gmdate("D, d M Y H:i:s")." GMT");

        // THIS HEADER MUST BE OMITTED FOR IE 6+
        if (FALSE === strpos($_SERVER["HTTP_USER_AGENT"], 'MSIE '))
        {
            header("Cache-Control: no-cache, must-revalidate");
        }

        // THIS IS THE LAST HEADER
        header("Pragma: no-cache");

        // FLUSH THE HEADERS TO THE BROWSER
        flush();

        // CAPTURE THE FILE IN THE OUTPUT BUFFERS - WILL BE FLUSHED AT SCRIPT END
        ob_start();
        echo $filedata;
    }

    // FAILURE
    else
    {
        die("ERROR: UNABLE TO OPEN $filename");
    }
}

【讨论】:

  • 很好的答案。按预期工作得很好。
  • 对我来说它没有下载它只是输出:错误:无法打开但使用了这个链接infosearchshop.com/59-php-script-for-downloading-files
  • @kinsleykajiva 如果 ($filedata) 在您的情况下返回 false,您可以在代码中看到。如果您的脚本无法访问文件,则无法下载。可能是@file_get_contents($filename);无法获取文件。删除 @ 以查看错误。最好检查文件路径。
【解决方案2】:

我从http://php.net/manual/en/function.readfile.php 复制了这个,现在可以使用了。 ob_clean();成功了..

    #setting headers
    header('Content-Description: File Transfer');
    header('Cache-Control: public');
    header('Content-Type: '.$type);
    header("Content-Transfer-Encoding: binary");
    header('Content-Disposition: attachment; filename='. basename($file));
    header('Content-Length: '.filesize($file));
    ob_clean(); #THIS!
    flush();
    readfile($file);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-31
    • 1970-01-01
    • 2010-10-31
    • 1970-01-01
    • 2016-07-30
    • 1970-01-01
    相关资源
    最近更新 更多