我试图让这段代码工作,但由于某种原因,所有的echo都能够输出正确的内容,但标题似乎不想强制下载我的文档。下面是我试图为文件下载构建的文件。设置为输入如下代码:downloader.php?f=13&t=doc
,根据用户权限从两个文件夹中的一个下载命名为201-xxx.doc
或201-xxx.pdf
的文件。
所有的逻辑工作到底部的标题信息。如果我注释掉报头内容类型和报头内容配置,那么它将把文件读入浏览器。其中任何一行都包含在内,它给我一个错误,说"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新手,所以如果有什么我错过了,请分享。)提前感谢!
如果它是一个大文件,它不能用readfile
发送。试着这样写:
$handle = fopen($file_path, 'rb');
$buffer = '';
while (!feof($handle)) {
$buffer = fread($handle, 4096);
echo $buffer;
ob_flush();
flush();
}
fclose($handle);
我不知道为什么这工作,但我能够通过将我的php文件分成两部分来解决这个问题。第1部分加载WordPress并执行逻辑验证。然后文件1将信息传递给文件2执行下载逻辑并写入头信息。
就像我说的,我不知道为什么这工作,但是一个朋友谁知道PHP比我做的更好,有时如果脚本需要太长时间来处理,那么头不会采取。有可能WordPress挂起脚本的时间太长了。
如果您试图使用
强制浏览器下载文件Content-Type: application/octet-stream
Content-Disposition: attachment; filename="your-file.jpg"
但是Chrome给你ERR_FILE_NOT_FOUND和Firefox也失败与"未找到"(奇怪的Opera似乎工作)尝试添加:
header('HTTP/1.0 200 OK', true, 200);
Chrome告诉我:"错误6 (net::ERR_FILE_NOT_FOUND):文件或目录无法找到。"而Firefox显示文件不存在
虽然相同的php文件是处理不同类型的下载,我有PNG和ICO的问题,我尝试了一些方法,只显示图片,但不提示下载框。
最后我发现感谢Crazycoolcam, Wordpress是问题所在。我在一个名为tools.php的文件中加入了一个php,在tools.php中,它包含了wordpress的主头文件,为了解决这个问题,我把我的工具文件分成了一个wordpress版本和一个非wordpress版本,并在wordpress将文件写出来后包含了wordpress的一半。
这是为什么它不起作用的另一种可能性。这就是我的原因。有趣的是,file_exists返回true,但是如果没有正确设置以下参数,任何形式的供公众下载的文件都不能工作。
PHP有一个设置叫做open_basedir
确保此设置与您的托管环境正确相关。Open_basedir可以通过php.ini