PHP-简单的下载脚本


PHP - simple download script

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

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

当我下载该文件时,无论我下载了什么文件类型,它都包含了我的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;

试试这个函数,或者将这些头实现到代码中

    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");
    }
}

我从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);