PHP正在读取文件而不是下载 [英] PHP is reading file instead of downloading

查看:163
本文介绍了PHP正在读取文件而不是下载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我下面有用于使用PHP从文件夹下载文件的脚本,它可以在本地工作,但在网上可以在下载状态下读取和打印。

  if($ result&& count($ result)> 0){
$ row = $ result [0];
$ book = true;
$ Location =’./files/’;
$ bookLocation = $ Location。 $ row ['file_name']; //示例:report.zip
if(file_exists($ bookLocation)){
$ fileLocation = $ bookLocation;
$ file_size = filesize($ fileLocation);
echo‘请等待下载文件...。
标头(内容描述:文件传输);
标头( Content-Type:应用程序/强制下载);
标头( Content-Disposition:附件;文件名=。$ row ['file_name']));
标头( Content-Transfer-Encoding:二进制);
标头(有效期:0);
标头( Cache-Control:必须重新验证,post-check = 0,pre-check = 0);
标头( Pragma:公开);
标头(‘Content-Length:’。$ file_size);
ob_clean();
flush();
readfile($ fileLocation);
出口;
} else {
echo‘< h3>抱歉!,找不到文件...< / h3>’;
}
}

.htaccess脚本

  
上的RewriteEngine
RewriteRule ^ $ index.html
RewriteRule ^ index.html $ index.php

RewriteRule ^([0-9] {1,9})(/)([^ / \。] + [^ / \。] +)(_ + [^ / \。] +) (\.html |)/?$ index.php?id = $ 1& cat = $ 3
RewriteRule ^([^ / \。] + [^ / \。] +)(\。 html |)/?$ index.php?page = $ 1
RewriteRule ^(。*)(/)([0-9] {1,9})(/)(。*)(\.html |)/?$ index.php?view_file = $ 3& file = $ 5
RewriteRule ^(。*)(/)([0-9] {1,9})(/)(。*)( ht.htm)/?$ index.php?download = $ 3

RewriteRule ^(作者)(/)(。*)(。html)/?$ index.php?user = $ 1

感谢您的帮助。

解决方案

在某些浏览器中,您可能会将其设置为自动下载并运行该特定文件类型。



如果是这样,没有办法解决这个问题。浏览器会决定如何处理文件,即使您告诉它下载文件,用户也可能已经告诉它可以直接运行。



大编辑



虽然我先前的评论是正确的,但是您的代码中存在一个主要问题:

  $ file_size = filesize($ fileLocation); 
echo‘请等待下载文件...。
标头(内容描述:文件传输);

必须删除回声语句。它势必会引起问题,例如没有发送标头。考虑到您的描述,我想这就是您的问题。



请参见,一旦发送任何内容,您无法发送更多标头。 / p>

I have script below for downloading files from a folder using PHP, it is working local but online it is reading and printing on the page instate of downloading.

if( $result && count($result) > 0){
    $row = $result[0];
    $book = true;
    $Location = './files/';
    $bookLocation = $Location . $row['file_name']; // exampe: report.zip
    if( file_exists( $bookLocation ) ){
        $fileLocation = $bookLocation;
        $file_size = filesize($fileLocation);
        echo 'Please wait downloading the file....';
        header('Content-Description: File Transfer');
        header('Content-Type: application/force-download');
        header('Content-Disposition: attachment; filename=' . $row['file_name']);
        header('Content-Transfer-Encoding: binary');
        header('Expires: 0');
        header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
        header('Pragma: public');
        header('Content-Length: ' . $file_size);
        ob_clean();
        flush();
        readfile($fileLocation);
        exit;      
    }else{
        echo '<h3>Sorry!, file not found...</h3>';
    }
}

.htaccess script

RewriteEngine on

RewriteRule ^$ index.html
RewriteRule ^index.html$ index.php

RewriteRule ^([0-9]{1,9})(/)([^/\.]+[^/\.]+)(_+[^/\.]+)(\.html|)/?$ index.php?id=$1&cat=$3
RewriteRule ^([^/\.]+[^/\.]+)(\.html|)/?$ index.php?page=$1
RewriteRule ^(.*)(/)([0-9]{1,9})(/)(.*)(\.html|)/?$ index.php?view_file=$3&file=$5
RewriteRule ^(.*)(/)([0-9]{1,9})(/)(.*)(\.htm)/?$ index.php?download=$3

RewriteRule ^(author)(/)(.*)(.html)/?$ index.php?user=$1

Thanks for the help.

解决方案

It is possible that in certain browsers, you set it to automatically download and run that particular file type.

If that is the case, there is no way to "fix" this. The browser decides what to do with the file, even if you tell it to download it, the user might have told it to run directly.

Big Edit

While my earlier comment is correct, you have a major issue in your code:

    $file_size = filesize($fileLocation);
    echo 'Please wait downloading the file....';
    header('Content-Description: File Transfer');

You must remove that echo statement. It is bound to cause issues, such as the headers not being sent. Considering your description, I'd say this is your problem.

See, once you send any content, you cannot send more headers.

这篇关于PHP正在读取文件而不是下载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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