2012-03-15 83 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會這樣做。