2012-03-15 156 views
3

我正在创建页面,用户可以上传/下载存储在mysql数据库中的pdf文件,当我下载文件时,它变得损坏/损坏的问题为什么存储在mysql数据库中的PDF文件在下载时被损坏/损坏?

NP:存储为blob的文件数据数据库。

下面是我的代码:

 // Gather all required data 
     $name = $dbLink->real_escape_string($_FILES['uploaded_file']['name']); 
     $mime = $dbLink->real_escape_string($_FILES['uploaded_file']['type']); 
     $data = $dbLink->real_escape_string(file_get_contents($_FILES   ['uploaded_file']['tmp_name'])); 
     $size = intval($_FILES['uploaded_file']['size']); 

     // Create the SQL query 
     $query = " 
      INSERT INTO `file` (
       `name`, `mime`, `size`, `data`, `created` 
      ) 
      VALUES (
       '{$name}', '{$mime}', {$size}, '{$data}', NOW() 
      )"; 

     // Execute the query 
     $result = $dbLink->query($query); 

     // Check if it was successfull 
     if($result) { 
      echo 'Success! Your file was successfully added!'; 
     } 
     else { 
      echo 'Error! Failed to insert the file' 
       . "<pre>{$dbLink->error}</pre>"; 
     } 
    } 
    else { 
     echo 'An error accured while the file was being uploaded. ' 
      . 'Error code: '. intval($_FILES['uploaded_file']['error']); 
    } 

    // Close the mysql connection 
    $dbLink->close(); 
} 
else { 
    echo 'Error! A file was not sent!'; 
} 

download code: 
// Fetch the file information 
     $query = " 
      SELECT `mime`, `name`, `size`, `data` 
      FROM `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: application/pdf'); 
       header("Content-Type: application/force-download"); 
       header("Pragma: public"); 
       header("Content-Description: File Transfer"); 
       header("Content-Type: ".$row['mime']); 
       header("Content-Length: ".$row['size']); 
       header("Content-Disposition: attachment; filename=".$row['name']); 
       header("Content-Transfer-Encoding: binary"); 
       header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); 
       // Print data 
       @readfile($row['data']); 
      } 
      else { 
       echo 'Error! No file 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.'; 
} 
+1

糟糕的想法来存储在数据库中的文件内容,请参阅http://stackoverflow.com/questions/3748/storing-images-in-db-yea-or-nay – 2012-03-15 02:33:14

+1

@ Bubby4j这是一个学校项目,所以我没有选择:S – 2012-03-15 02:40:25

+0

保存为blob? – EGHDK 2012-07-08 01:53:17

回答

2

没有办法的办法。 Mysqli也不是这样。你总是有选择,告诉你的老师这种方法没有价值。为什么不将jpg存储在mysql数据库中?因为有更好的方法 - 将链接存储在数据库中,将.jpg存储在网页上。与PDF相同。如果您必须仔细阅读,请查看基于Web的编辑器如何使用ckedit存储数据(另一种错误方法),并查看dompdf code.google.com/p/dompdf/以了解什么是PDF格式。

为什么mysqli是一个坏主意? mySql就是它。 mySql试图使其他方法更好地完成其他方法,否则mysql会这样做。