PHP readfile()显示文件内容,而不是强制浏览器下载文件

时间:2017-07-06 21:14:23

标签: php file readfile

这是我第一次尝试使用php向用户发送文件,我有以下设置:

if (file_exists($fullPath)) {

    header('Content-type: text/csv');
    //Use Content-Disposition: attachment to specify the filename
    header('Content-Disposition: attachment; filename='.basename($fullPath));
    //No cache
    header('Expires: 0');
    header('Cache-Control: must-revalidate');
    header('Pragma: public');

    //Define file size
    header('Content-Length: ' . filesize($fullPath));

    ob_clean();
    flush();
    readfile($fullPath);
}else{
    echo "alert(\"File name is NOT FOUND: ".$fullPath."\");";
}

这会抓取文件,但它会在网页上显示该文件,就好像我回显了文件内容一样。保存此代码的脚本包含在require语句中。如果我通过浏览器导航到手动调用此单个文件,则会下载该文件,但如果此脚本作为必需语句包含在另一个脚本中,则该文件不会下载但只会输出到屏幕。

我怀疑标题可能存在一些问题,但我不确定这是不是问题。

0 个答案:

没有答案
相关问题