下载代码(readfile)给我一个空的或损坏的文件
我正在尝试构建一个下载页面,该页面获取文件 ID 并通过从数据库获取文件信息来生成下载链接,
这有点有效!由于某种原因,我从该页面获取了一个文件,但它并不是真正的文件! 例如,当我将图片ID发送到此页面时,将下载图片,但它不会打开,而且它的大小实际上比实际的要小,
这是我的代码,
<?php
require_once('class/comon.php');
require_once('class/db.php');
$file_id = isset($_GET['id']) && (int)$_GET['id'] != 0 ? (int)$_GET['id'] : exit;
$file = comon::get_fileinfo('files' , 'id' , $file_id );
if(!$file) exit;
foreach($file as $file){
$location = $file['location'];
$filename = $file['file_name'];
}
$ext = end(explode('.' , $filename ));
header( 'Pragma: public' );
header( 'Expires: 0' );
header( 'Cache-Control: must-revalidate, post-check=0, pre-check=0' );
header( 'Cache-Control: private', false );
header( 'Content-Type: application/'.$ext);
header( 'Content-Disposition: attachment; filename="'. basename($filename) . '";' );
header( 'Content-Transfer-Encoding: binary' );
header( 'Content-Length: ' . filesize( 'http://localhost/Lancer/attach/'.$location.'/'.$filename ) );
readfile( 'http://localhost/Lancer/attach/'.$location.'/'.$filename );
?>
我在页面上打印了文件路径
echo 'localhost/Lancer/attach/'.$location.'/'.$filename;
,然后我将输出放在网址并显示图片,所以这不是问题
i'm trying to build a download page which gets the file id and the generates download link by getting files information from database
it kinda works ! for some reason i get a file from this page but it's not really a file !
for example when i send a picture id to this page a picture will be downloaded but it wont open and it's size is really smaller than what really is
here is my code
<?php
require_once('class/comon.php');
require_once('class/db.php');
$file_id = isset($_GET['id']) && (int)$_GET['id'] != 0 ? (int)$_GET['id'] : exit;
$file = comon::get_fileinfo('files' , 'id' , $file_id );
if(!$file) exit;
foreach($file as $file){
$location = $file['location'];
$filename = $file['file_name'];
}
$ext = end(explode('.' , $filename ));
header( 'Pragma: public' );
header( 'Expires: 0' );
header( 'Cache-Control: must-revalidate, post-check=0, pre-check=0' );
header( 'Cache-Control: private', false );
header( 'Content-Type: application/'.$ext);
header( 'Content-Disposition: attachment; filename="'. basename($filename) . '";' );
header( 'Content-Transfer-Encoding: binary' );
header( 'Content-Length: ' . filesize( 'http://localhost/Lancer/attach/'.$location.'/'.$filename ) );
readfile( 'http://localhost/Lancer/attach/'.$location.'/'.$filename );
?>
i've printed file path on the page
echo 'localhost/Lancer/attach/'.$location.'/'.$filename;
and then i put the output on the url and it shows the picture so that cant be the problem
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看起来您需要
readfile('http://localhost/....')
或文件的绝对本地文件路径。要使用 URL 字符串,您需要提供读取文件的协议。
It looks like you want either
readfile('http://localhost/....')
or the absolute local file path to the file.To use the URL string you need to provide a protocol to readfile.