我試圖讓這段代碼的工作,但由於某些原因,所有的回波的能夠輸出正確的內容,但沒有頭似乎不想強制下載我的文檔。接下來是我正在嘗試構建的用於文件下載的文件。它被設置爲輸入如下代碼: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新手,所以如果有什麼我不見了,請分享。)
在此先感謝!
使用'is_readable()'而不是'file_exists()'會更安全。 is_readable既能確保文件存在,又能讀取文件。有很多文件存在,但無法讀取。另外,當你調用readfile時,'$ file_path'中有什麼? –
剛剛看到這個評論/問題。我切換到'is_readable()',它仍然會拋出錯誤。當我取消註釋$ file_path時,它會迴應這個:'/ home5/[我的主機帳戶]/public_html/1hoefl4priaspoafr/ex/201-015.pdf'這是該文件的正確目錄。大文件的 – Crazycoolcam