PHP Readfile()不适用于我,我不知道为什么 [英] PHP Readfile() not working for me and I don't know why

查看:119
本文介绍了PHP Readfile()不适用于我,我不知道为什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图让这段代码工作,但由于某种原因,所有的echo都能够输出正确的内容,但是标头似乎不想强制下载我的文档。以下是我正在为文件下载而构建的文件。它设置为输入如下代码: downloader.php?f = 13& t = doc 以下载名为 201-xxx的文件。 code>或 201-xxx.pdf 从两个文件夹之一取决于用户权限。

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.

所有的逻辑都可以到底部的标题信息。如果我注释了标题内容类型和标题内容处置,那么它会将文件读入浏览器。包含这些行之一,它给我一个错误,说错误6(net :: ERR_FILE_NOT_FOUND):找不到文件或目录。

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."); }


?>

请帮助我一整天都在这里,我很困惑,为什么这不会工作。 Filesize()拉出正确的长度,所以我知道我正在查看的路径中有一个文件。 (我也是PHP的新手,所以如果有一些我失踪的东西,请分享。)

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.)

提前感谢! p>

Thanks in advance!

推荐答案

我不知道为什么这样工作,但是我可以通过将我的php文件分成两部分来解决这个问题。第1部分加载WordPress并执行逻辑验证。然后文件1将信息传递给文件2,执行下载逻辑并写入标题信息。

I am not sure why this worked, but I was able to solve this issue by breaking my php file into two pieces. Piece 1 loads WordPress and performs the logic validation. Then file 1 passes the information over to file 2 to do the download logic and write the header information.

像我所说,我不知道为什么这样工作,但是知道PHP的朋友比我说的更好,有时如果脚本需要太长时间才能处理,那么头文件不会占用。可能WordPress挂起脚本太长了这些头。

Like I said, I am not sure why this worked, however a friend who knows PHP better than I do said that sometimes if the script takes too long to process then the headers won't take. It is possible that WordPress was hanging the script too long for these headers.

希望这个解释将帮助有困难的人。

Hopefully this explanation will help someone else who is having this difficulty.

这篇关于PHP Readfile()不适用于我,我不知道为什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆