PHP READFILE()不是为我工作,我不知道为什么
我试图让这段代码的工作,但由于某些原因,所有的回波的能够输出正确的内容,但没有头似乎不想强制下载我的文档。接下来是我正在尝试构建的用于文件下载的文件。它被设置为输入如下代码:downloader.php?f=13&t=doc
根据用户权限从两个文件夹之一下载名为201-xxx.doc
或201-xxx.pdf
的文件。PHP READFILE()不是为我工作,我不知道为什么
所有的逻辑工作到底部的标题信息。如果我注释掉标题内容类型和标题内容处置,那么它会将文件读入浏览器。和这两个行在内,它给我,说"Error 6 (net::ERR_FILE_NOT_FOUND): The file or directory could not be found."
<?php
//ob_start();
if (!defined('__DIR__')) define('__DIR__', dirname(__FILE__));
define("TLOJ_FSROOT", __DIR__ . "/");
define('WP_USE_THEMES', false);
require('./wp-blog-header.php');
$lessonnumber = $_REQUEST['f'];
$type = $_REQUEST['t'];
if ($lessonnumber < '10') { $threedigitlesson = '00' . $lessonnumber; }
elseif ($lessonnumber < '100') { $threedigitlesson = '0' . $lessonnumber; }
else { $threedigitlesson = $lessonnumber; }
$filenamestart = "201-" . $threedigitlesson;
$contenttype = 'application/octet-stream';
switch ($type) {
case 'pdf':
$extension = '.' . $type;
$contenttype = 'application/pdf';
break;
case 'doc':
$extension = '.' . $type;
$contenttype = 'application/msword';
break;
default:
$contenttype = '';
exit("It appears that you are trying to download a file that is not a lesson document. Please contact us if you believe this to be an error.");
}
$filename = $filenamestart . '.' . $type;
$current_user = wp_get_current_user();
//$siteurl = site_url();
$pathroot = TLOJ_FSROOT;
$download_path = $pathroot . "1hoefl4priaspoafr/";
if (current_user_can("access_s2member_ccap_extendedlessons")) {
$download_path = $download_path . "ex/";
} else {
$download_path = $download_path . "st/";
}
$file_path = $download_path . $filename;
$tlojmemberlength = tlojunlocklessons();
if (!is_user_logged_in()) { exit("Please log in to access the file"); }
if (!current_user_can("access_s2member_ccap_downloadlessons")) { exit("You don't have access to download the lessons!"); }
if ($lessonnumber > $tlojmemberlength) { exit("It appears you are trying to jump ahead! While I am excited at your enthusiam, let's not rush our study time."); }
if (($lessonnumber > '195') && (!current_user_can("access_s2member_ccap_lastweek"))) { exit("Upgrade now to access the downloads for the five bonus lessons!"); }
// build Final File Name
$extendedmessage = "";
if (current_user_can("access_s2member_ccap_extendedlessons")) { $extendedmessage = " - Extended"; }
$myfinishedlessonname = "Lesson " . $lessonnumber . $extendedmessage . " -- The Life of Jesus Study" . "." . $type;
// echo 'Download Path: ' . $download_path . '<br />';
// echo 'Source/Lesson Number: ' . $lessonnumber . '<br />';
// echo 'File Name: ' . $filename . '<br />';
// echo 'File Type: ' . $type . '<br />';
// echo 'Allowed Lessons: ' . $tlojmemberlength . '<br />';
// echo 'Final File Name: ' . $myfinishedlessonname . '<br />';
// echo 'File Path: ' . $file_path . '<br />';
// echo 'Content Type: ' . $contenttype . '<br />';
// echo 'File Size: ' . filesize($file_path) . '<br />';
if (headers_sent()) { exit("Sorry but the headers have already been sent."); }
ob_end_clean();
if (file_exists($file_path)) {
header('Content-Description: File Transfer');
header('Content-type: ' . $contenttype);
header('Content-disposition: attachment; filename="' . $myfinishedlessonname . '"');
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: ');
header('Pragma: ');
header('Content-Length: ' . filesize($file_path));
flush();
ob_clean();
readfile($file_path);
exit;
} else { exit("No file present."); }
?>
请帮助,因为我一直在这个整天感到困惑没有结束,为什么这是行不通的错误。 Filesize()拉正确的长度,所以我知道我正在看的路径中有一个文件。 (我也是PHP新手,所以如果有什么我不见了,请分享。)
在此先感谢!
我不知道为什么这个工作,但我能解决这个问题,通过打破我的PHP文件分成两部分。第1部分加载WordPress并执行逻辑验证。然后文件1将信息传递给文件2以执行下载逻辑并写入标题信息。
就像我说的,我不知道为什么这个工作,但是谁知道不是我不说,有时如果脚本时间太长来处理,然后头也不会采取PHP更好的朋友。 WordPress可能会将这些脚本挂起的时间过长。
希望这个解释能帮助别人谁有这个困难。
我以前也必须这样做。只有我可以去工作的事情。 –
如果它是一个大的文件,它不能与readfile
发送。尝试使用这个:
$handle = fopen($file_path, 'rb');
$buffer = '';
while (!feof($handle)) {
$buffer = fread($handle, 4096);
echo $buffer;
ob_flush();
flush();
}
fclose($handle);
你的意思是大于1兆。我所有的PDF和DOC都不到100k。 我应该用这段文字替换标题吗? – Crazycoolcam
我试图只替换readfile行,错误仍然出现。 我也试着注释掉所有的标题行来支持这段代码,并且它在浏览器中传递了这个文件。 – Crazycoolcam
非常感谢!为了使你的代码具有可移植性,必须让ob_flush和flush被调用。我花了3小时寻找为什么函数不工作:) tnx! –
Chrome的告诉我:“错误6(网:: ERR_FILE_NOT_FOUND):文件或目录无法找到。” 和Firefox一样,这个文件并不存在。
虽然同PHP文件处理不同类型的我是有PNG和ICO问题下载,我尝试了一些方法,只有显示的图片,但没有提示需要下载框。
最后我发现感谢Crazycoolcam,Wordpress是问题。 我包括PHP到一个文件中我曾呼吁“tools.php”, tools.php它里面有一个包括使用WordPress的主头文件, 弥补我分裂这个问题我的工具文件到WordPress版本和非WordPress的版本,并在写出文件后包含wordpress的一半。
很高兴看到这为您解决。我已经调整了我的一套两个文件,但解决方案仍然是两个文件。 我也刚刚发现wordpress使用自己的一系列标签,'?s = xx'不起作用,因为我相信WordPress使用's'变量。我还没有深入了解哪些字母组合不好,但我知道有一些。 很高兴您已启动并重新运行,这有助于解决您的问题。 – Crazycoolcam
如果您试图强制浏览器使用 下载文件'Content-Type:application/octet-stream Content-Disposition:attachment;文件名=“your-file.jpg”' 但Chrome会给你** ERR_FILE_NOT_FOUND **并且Firefox也会因为“未找到”而失败(奇怪的是Opera似乎有效)尝试添加: 'header('HTTP/1.0 200 OK',true,200);' –
如果你试图迫使浏览器下载文件,使用
Content-Type: application/octet-stream
Content-Disposition: attachment; filename="your-file.jpg"
而Chrome为您提供了ERR_FILE_NOT_FOUND和Firefox也失败,“未找到”(奇怪的歌剧似乎工作)尝试添加:
header('HTTP/1.0 200 OK', true, 200);
这解决了我的问题。我想这是因为我在URL重写中处理这个,所以技术上状态默认为404,然后将其正确设置为200。 –
只是另一种可能性,此间为什么它不工作。这是我的原因。有趣的是,file_exists返回true,但没有任何形式的文件供公众下载使用,但没有正确设置下面的内容。
PHP有一个叫做的open_basedir
确认这是正确的相关设置为您的托管环境设置。 open_basedir可以通过php.ini编辑
使用'is_readable()'而不是'file_exists()'会更安全。 is_readable既能确保文件存在,又能读取文件。有很多文件存在,但无法读取。另外,当你调用readfile时,'$ file_path'中有什么? –
刚刚看到这个评论/问题。我切换到'is_readable()',它仍然会抛出错误。当我取消注释$ file_path时,它会回应这个:'/ home5/[我的主机帐户]/public_html/1hoefl4priaspoafr/ex/201-015.pdf'这是该文件的正确目录。大文件的 – Crazycoolcam