PHP Readfile()不适合我,我不知道为什么

Cra*_*cam 1 php download readfile

我试图让这个代码工作,但由于某种原因,所有的回声都能够输出正确的内容,但标题似乎不想强制下载我的文档.以下是我试图为文件下载构建的文件.它被设置为输入如下代码:根据用户权限downloader.php?f=13&t=doc下载已命名的文件201-xxx.doc201-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."); }


?>
Run Code Online (Sandbox Code Playgroud)

请帮助我,因为我一直在这一整天都很困惑,我很困惑,为什么这不起作用.Filesize()拉出正确的长度,所以我知道我正在查看的路径中有一个文件.(我也是PHP的新手,所以如果有什么我不知道的,请分享.)

提前致谢!

sam*_*ura 8

如果它是一个大文件,则无法发送readfile.试着用这个:

  $handle = fopen($file_path, 'rb'); 
  $buffer = ''; 
  while (!feof($handle)) { 
    $buffer = fread($handle, 4096); 
    echo $buffer; 
    ob_flush(); 
    flush(); 
  } 
  fclose($handle); 
Run Code Online (Sandbox Code Playgroud)