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

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

WBOY
WBOYOriginal
2016-06-23 14:10:56623browse

当我点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>


回复讨论(解决方案)

$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);
 }

nbsp;html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">



资料下载

 



把这些放到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>

那些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);}

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

Statement:
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