2016-11-07 80 views
0

我有PHP代码从MySQL数据库downloding文件,但是当我点击链接会出现以下错误,下载犯错

警告:不能更改头信息 - 头已经发出通过 (输出开始于/home/somelocation/loc/coks/getfiles.php:2)“。

<?php 
// Make sure an ID was passed 
if(isset($_GET['id'])) { 
// Get the ID 
    $id = intval($_GET['id']); 

    // Make sure the ID is in fact a valid ID 
    if($id <= 0) { 
     die('The ID is invalid!'); 
    } 
    else { 
     // Connect to the database 
     $dbLink = new mysqli('localhost', 'username', '[email protected]', 'Databases'); 
     if(mysqli_connect_errno()) { 
      die("MySQL connection failed: ". mysqli_connect_error()); 
     } 

     // Fetch the file information 
     $query = " 
      SELECT `mime`, `name`, `size`, `data` 
      FROM `resume_file` 
      WHERE `id` = {$id}"; 
     $result = $dbLink->query($query); 

     if($result) { 
      // Make sure the result is valid 
      if($result->num_rows == 1) { 
      // Get the row 
       $row = mysqli_fetch_assoc($result); 

       // Print headers 
       header("Content-Type: ". $row['mime']); 
       header("Content-Length: ". $row['size']); 
       header("Content-Disposition: attachment; filename=". $row['name']); 

       // Print data 
       echo $row['data']; 
      } 
      else { 
       echo 'Error! No image exists with that ID.'; 
      } 

      // Free the mysqli resources 
      @mysqli_free_result($result); 
     } 
     else { 
      echo "Error! Query failed: <pre>{$dbLink->error}</pre>"; 
     } 
     @mysqli_close($dbLink); 
    } 
} 
else { 
    echo 'Error! No ID was passed.'; 
} 
?> 

什么是在上面的代码中的问题,请,谁能帮助我。 注意:请检查第32,33,34行。

谢谢

回答

-1

我们已经有这个答案。

尝试此链接。

How to fix "Headers already sent" error in PHP

这是由于在我们的代码的空白字符。检查网址了解更多详情

output started at /home/somelocation/loc/coks/getfiles.php:2 

u有你的 <?php 这就是之前的一些空白,为什么错误显示像loc/coks/getfiles.php:2<?php从线2号仅 :)

+0

谁不喜欢我的文章的一个开始,请通过链接.. –

+0

固定。谢谢你。 – Sathishkumar