php文件强力下载 [英] php file force download

查看:140
本文介绍了php文件强力下载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我使用此代码下载此图像(仅用于测试目的)时,我打开下载的图像,所有它给我的是一个错误。我试过在铬。打开它与Windows照片查看器,它说,它不能显示图片,因为它是空的?
这里是代码:

 <?PHP 
//定义文件路径
$ file ='http://www.media.lonelyplanet.com/lpi/12553/12553-11/469x264.jpg';

if(!file)
{
//文件不存在,输出错误
die('file not found');
}
else
{
header('Content-Description:File Transfer');
header('Content-Type:application / octet-stream');
header('Content-Disposition:attachment; filename ='。basename($ file));
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:'。filesize($ file));
ob_clean();
flush();
readfile($ file);
退出;
}
?>


解决方案

我有机会解决这个问题。您的问题是双重的。



首先,从网址中删除 www。

其次,删除对 filesize($ file)的调用,因为PHP不知道文件的大小在它下载文件之前。 (真的,只是删除整行)



删除这两件事,我是成功的。


When I use this code to download this image (only used for testing purposes), I open the downloaded image, and all it gives me is an error. i tried it in chrome. opening it with windows photo viewer, it says that it can't display the picture because it is empty??? here is the code:

<?PHP
 // Define the path to file
 $file = 'http://www.media.lonelyplanet.com/lpi/12553/12553-11/469x264.jpg';

 if(!file)
 {
     // File doesn't exist, output error
     die('file not found');
 }
 else
 {
     header('Content-Description: File Transfer');
        header('Content-Type: application/octet-stream');
        header('Content-Disposition: attachment; filename='.basename($file));
        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: ' . filesize($file));
        ob_clean();
        flush();
        readfile($file);
        exit;
 }
 ?>

解决方案

I've had a chance to work it out. Your problem is two-fold.

First, remove the www. from the url.

Second, remove the call to filesize($file) which is throwing an error because PHP doesn't know the size of the file before it downloads the file. (really, just remove the whole line)

Removing these two things, I was successful.

这篇关于php文件强力下载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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