Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
561 views
in Technique[技术] by (71.8m points)

download - PHP Readfile() not working for me and I don't know why

I am trying to get this code to work but for some reason, all the echo's are able to output correct content, but the headers don't seem to want to force the download of my document. What follows is the file I am trying to build for file downloads. It is set to input code like this: downloader.php?f=13&t=doc to download a file that is named 201-xxx.doc or 201-xxx.pdf from one of two folders depending on the users privileges.

All the logic works up to the header info at the bottom. If I comment out the header content type and the header content disposition, then it will read the file into the browser. With either of those lines included, it give me an error that says "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."); }


?>

Please help as I have been at this all day and am confused to no end why this won't work. Filesize() pulls the correct length so I know there is a file in the path that I am looking at. (I am also new to PHP, so if there is something that I am missing, please share.)

Thanks in advance!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

If it's a big file, it cannot be sent with readfile. Try to use this:

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...