PHP readfile()导致文件下载损坏

PHP readfile()导致文件下载损坏,php,file-io,download,Php,File Io,Download,我正在使用php脚本提供从我的网站下载后,一个必要的javascript定时器这个php脚本是导致下载。但无论我怎么做,下载的文件都已损坏。谁能帮我指出哪里出了问题 这是我的密码 <?php include "db.php"; $id = htmlspecialchars($_GET['id']); $error = false; $conn = mysql_connect(DB_HOST,DB_USER,DB_PASSWORD); if(!($con

我正在使用php脚本提供从我的网站下载后,一个必要的javascript定时器这个php脚本是导致下载。但无论我怎么做,下载的文件都已损坏。谁能帮我指出哪里出了问题

这是我的密码

     <?php
include "db.php";    
 $id = htmlspecialchars($_GET['id']);
 $error = false;
    $conn = mysql_connect(DB_HOST,DB_USER,DB_PASSWORD);
    if(!($conn)) echo "Failed To Connect To The Database!";
    else{   
        if(mysql_select_db(DB_NAME,$conn)){
            $qry = "SELECT Link FROM downloads WHERE ID=$id";
            try{
                $result = mysql_query($qry);
                if(mysql_num_rows($result)==1){
                    while($rows = mysql_fetch_array($result)){
                        $f=$rows['Link'];
                    }
                    //pathinfo returns an array of information
                    $path = pathinfo($f);
                    //basename say the filename+extension
                    $n = $path['basename'];
                    //NOW comes the action, this statement would say that WHATEVER output given by the script is given in form of an octet-stream, or else to make it easy an application or downloadable
                    header('Content-type: application/octet-stream');
                    header('Content-Length: ' . filesize($f));
                    //This would be the one to rename the file
                    header('Content-Disposition: attachment; filename='.$n.'');
                    //Finally it reads the file and prepare the output
                    readfile($f);
                    exit();
                }else $error = true;
            }catch(Exception $e){
                $error = true;
            }
            if($error) 
            {
                header("Status: 404 Not Found");
                }
        }
  }
?> 


首先,正如一些人在评论中指出的那样,在打开PHP标记之前删除所有空格(
这对我在打开更多输出缓冲区的情况下有所帮助

//NOW comes the action, this statement would say that WHATEVER output given by the script is given in form of an octet-stream, or else to make it easy an application or downloadable
header('Content-type: application/octet-stream');
header('Content-Length: ' . filesize($f));
//This would be the one to rename the file
header('Content-Disposition: attachment; filename='.$n.'');
//clean all levels of output buffering
while (ob_get_level()) {
    ob_end_clean();
}
readfile($f);
exit();

请不要像在中那样使用
mysql.*
函数。请使用或改为使用and。在
开始之前有5个空格吗?删除开始php标记之前的任何空格,用
ob.\u start
启动脚本,在
readfile
之前执行
ob.\u clean
。我正在学习PDO…谢谢你的建议。顺便说一下这个脚本运行在000webhost上,它不太可能因为免费的WebHost服务而很快改变PHP版本…这是因为PHP标签之前的空间…撞了我的头,但我自己找不到它…非常感谢你…及时的帮助…另外,如果你遇到这个问题,请确保你是
flush()
ing输出缓冲区,如中所示。我已经寻找这样的答案好几天了……非常感谢,您为我节省了这么多时间!这是我问题的解决方法:下载后缺少几个字节。我不知道它们之间的关系,但这解决了我的问题。嘿,谢谢您的回答-即使这是一个非常古老的问题。如果您可以在代码片段周围添加更多信息,以帮助找到此答案的任何人理解您的建议以及原因,那将是非常棒的。
           ob_start();//add this to the beginning of your code 

      if (file_exists($filepath) && is_readable($filepath) ) {
header('Content-Description: File Transfer');
 header("Content-Type: application/octet-stream");
 header("Content-Disposition: attachment; filename=$files");
 header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
   header('Pragma: public');
 header("content-length=".filesize($filepath));
 header("Content-Transfer-Encoding: binary");

   /*add while (ob_get_level()) {
       ob_end_clean();
         }  before readfile()*/
  while (ob_get_level()) {
ob_end_clean();
   }
    flush();
   readfile($filepath);