设计了一个php下载当前文件,却把php源文件下载下来了,为何?

WBOY
Release: 2016-06-23 14:10:56
Original
629 people have browsed it

当我点a.txt下载后,打开txt文件,发现里面的内容不是a.txt本身的内容,而是该php文件中除了php代码的其他文本内容,这样该如何解决?

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=gb2312" /><title>资料下载</title></head><body><?php $file_name="a.txt";$file_dir="./";if(!file_exists($file_dir.$file_name)){ echo "文件找不到";   exit;}else{$file=fopen($file_dir.$file_name,"r");//打开文件	echo($file_dir.$file_name); Header("Content-type: application/octet-stream");   Header("Accept-Ranges:bytes");   Header("Accept-Length:".filesize($file_dir.$file_name));   Header("Content-Disposition: attachment; filename=".$file_name); }?></body></html>
Copy after login


回复讨论(解决方案)

$file_name="a.txt";
$file_dir="./";
if(!file_exists($file_dir.$file_name)){
echo "文件找不到";
exit;
}else{
$file=fopen($file_dir.$file_name,"r");//打开文件
//echo($file_dir.$file_name);
Header("Content-type: application/octet-stream");
Header("Accept-Ranges:bytes");
Header("Accept-Length:".filesize($file_dir.$file_name));
Header("Content-Disposition: attachment; filename=".$file_name);
readfile($file_dir.$file_name);
}





资料下载





把这些放到if里。

$file_name="a.txt";
$file_dir="./";
if(!file_exists($file_dir.$file_name)){
echo "文件找不到";
exit;
}else{
$file=fopen($file_dir.$file_name,"r");//打开文件
//echo($file_dir.$file_name);
Header("Content-type: application/octet-stream");
Header("Accept-Ranges:bytes");
Header("Accept-Length:".filesize($file_dir.$file_name));
Header("Content-Disposition: attachment; filename=".$file_name);
readfile($file_dir.$file_name);
} 我照你那样的改了,可以显示出内容了,但是是显示在body里……

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=gb2312" /><title>资料下载</title></head><body>这是我的内容。</body></html>
Copy after login

那些html标签就不要了,就像我#1的那样。

因为你的是下载,所以程序中的一切输出,都是被下载文件的内容
尽管你的 Header 函数是在后面执行的,但 php 会将其调动到响应的 http 头中
程序中你只打开了文件,但没有读取和输出文件内容
至少要写作

<?php $file_name="a.txt";$file_dir="./";if(!file_exists($file_dir.$file_name)){ echo "文件找不到";   exit;}else{ Header("Content-type: application/octet-stream");   Header("Accept-Ranges:bytes");   Header("Accept-Length:".filesize($file_dir.$file_name));   Header("Content-Disposition: attachment; filename=".$file_name); readfile($file_dir.$file_name);}
Copy after login

那些html标签就不要了,就像我#1的那样。 嗯,问题解决了,谢谢!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!