php file force download(php文件强制下载)
问题描述
当我使用此代码下载此图像(仅用于测试目的)时,我打开下载的图像,它给我的只是一个错误.我在 chrome 中试过了.用windows photo viewer 打开它,它说它无法显示图片,因为它是空的???这是代码:
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.
首先,从 url 中删除 www.
.
First, remove the www.
from the url.
其次,删除对 filesize($file)
的调用,该调用会引发错误,因为 PHP 在下载文件之前不知道文件的大小.(真的,只需删除整行)
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文件强制下载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:php文件强制下载
- Oracle 即时客户端 DYLD_LIBRARY_PATH 错误 2022-01-01
- 如何从数据库中获取数据以在 laravel 中查看页面? 2022-01-01
- openssl_digest vs hash vs hash_hmac?盐与盐的区别HMAC? 2022-01-01
- 使用 GD 和 libjpeg 支持编译 PHP 2022-01-01
- 覆盖 Magento 社区模块控制器的问题 2022-01-01
- Laravel 5:Model.php 中的 MassAssignmentException 2021-01-01
- PHP foreach() 与数组中的数组? 2022-01-01
- 如何在 Symfony2 中正确使用 webSockets 2021-01-01
- PHP - if 语句中的倒序 2021-01-01
- 如何使用 Google API 在团队云端硬盘中创建文件夹? 2022-01-01