2012-04-18 137 views
0

我目前有一段時間上傳圖像到我的數據庫。我目前有多個變量/輸入從一個表單上傳 - 如果這些輸入是圖像文件上傳的話。該文件似乎使它做數據庫,但是當我嘗試通過PHP腳本檢索圖像時,它只是返回「Array」,而不是圖像。任何幫助?謝謝!圖片/文件上傳到mysql數據庫不起作用

這裏的上傳代碼:

   // if the form's submit button is clicked, we need to process the form 
      if (isset($_POST['submit'])) 
      { 
        // get the form data 
          $projectname = htmlentities($_POST['projectname'], ENT_QUOTES); 
          $item = htmlentities($_POST['item'], ENT_QUOTES); 
          $description = htmlentities($_POST['description'], ENT_QUOTES); 
          $neededby = htmlentities($_POST['neededby'], ENT_QUOTES); 
          $shipping= htmlentities($_POST['shipping'], ENT_QUOTES); 
          $revisions = htmlentities($_POST['revisions'], ENT_QUOTES); 
          $price = htmlentities($_POST['price'], ENT_QUOTES); 
          $paid = htmlentities($_POST['paid'], ENT_QUOTES); 
          $ordered1 = htmlentities($_POST['ordered1'], ENT_QUOTES); 
          $ordered2 = htmlentities($_POST['ordered2'], ENT_QUOTES); 
          $ordered3 = htmlentities($_POST['ordered3'], ENT_QUOTES); 
          $received1 = htmlentities($_POST['received1'], ENT_QUOTES); 
          $received2 = htmlentities($_POST['received2'], ENT_QUOTES); 
          $received3 = htmlentities($_POST['received3'], ENT_QUOTES); 
          $shipped1 = htmlentities($_POST['shipped1'], ENT_QUOTES); 
          $shipped2 = htmlentities($_POST['shipped2'], ENT_QUOTES); 
          $shipped3 = htmlentities($_POST['shipped3'], ENT_QUOTES); 
          $tracking = htmlentities($_POST['tracking'], ENT_QUOTES); 
          $delivered = htmlentities($_POST['delivered'], ENT_QUOTES); 
          $thestatus = htmlentities($_POST['thestatus'], ENT_QUOTES); 
          $photo=($_FILES['photo']); 




        if ($projectname == '') 
          { 
            // if they are empty, show an error message and display the form 
            $error = 'ERROR: Please fill in project name!'; 
            renderForm($projectname, $item, $description, $neededby, $shipping, $revisions, $price, $paid, $ordered1, $ordered2, $ordered3, $received1, $received2, $received3, $shipped1, $shipped2, $shipped3, $tracking, $delivered, $thestatus, $photo, $error, $id); 
          } 

          else 
          { 
          // insert the new record into the database 
          if ($stmt = $mysqli->prepare("INSERT todo (projectname, item, description, neededby, shipping, revisions, price, paid, ordered1, ordered2, ordered3, received1, received2, received3, shipped1, shipped2, shipped3, tracking, delivered, photo, thestatus) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)")) 
          { 
            $stmt->bind_param("sssssssssssssssssssss", $projectname, $item, $description, $neededby, $shipping, $revisions, $price, $paid, $ordered1, $ordered2, $ordered3, $received1, $received2, $received3, $shipped1, $shipped2, $shipped3, $tracking, $delivered, $photo, $thestatus); 
            $stmt->execute(); 
            $stmt->close(); 
          } 

          // show an error if the query has an error 
          else 
          { 
            echo "ERROR: Could not prepare SQL statement."; 
          } 



          // redirec the user 
          header("Location: main.php"); 
        } 

      } 

和文件檢索代碼:

<?php 
mysql_connect("localhost","MYUSER","MYPASS"); 
mysql_select_db("MYDB"); 
$query = "SELECT photo FROM todo where id=$id"; 
$result = MYSQL_QUERY($query); 
$data = MYSQL_RESULT($result,0,"photo"); 
Header("Content-type: $type"); 
print $data; 
?> 

MySQL的列是一個BLOB類型。

這裏是一個形象,你可以上我說的一些視覺效果: http://i.imgur.com/DYHHx.png

+0

爲什麼要將圖像存儲在數據庫中,而不是將文件保存到目錄並將文件的路徑保存到數據庫中,是否有特殊原因?保存文件路徑對於數據庫來說會比存儲整個圖像好得多。 – knittledan 2012-04-18 22:55:00

+1

這首先是因爲'$ _FILES ['photo']'*是一個包含有關上傳文件信息的數組。這不是上傳的文件本身。你有沒有嘗試[查閱手冊](http://php.net/manual/en/features.file-upload.php)關於文件上傳? – deceze 2012-04-18 22:56:29

+0

@knittledan我相信BLOB數據類型存儲在表格本身之外,並且表格只包含一個指向信息的指針。因此,將數據存儲在數據庫中不會導致普通查詢出現問題(儘管它可能會減慢備份和複製速度)。 – octern 2012-04-18 22:58:37

回答

1
$fileName = $_FILES['image']['name']; 
$tmpName = $_FILES['image']['tmp_name']; 
$fileSize = $_FILES['image']['size']; 
$fileType = $_FILES['image']['type']; 

$fp  = fopen($tmpName, 'r'); 
$photo = fread($fp, filesize($tmpName)); 
$photo = addslashes($photo); 
fclose($fp); 

if(!get_magic_quotes_gpc()) 
{ 
    $fileName = addslashes($fileName); 
} 


//and here your insert query as i remember you can try it 


HTML CODE: 

    <input type=\"file\" name=\"image\" /> 


and here is how you retrive it 

echo '<img src="data:image/jpeg;base64,' . base64_encode($row['imageContent']) . '" />'; 

但我不建議你這樣做,因爲它會使你的數據庫沒有加載快,所以保存圖像以一個文件夾,onlyt在DATABSE

記我已得到了一個論壇和唐該代碼的名稱;噸記得它的名字對不起

+0

爲什麼要向二進制文件添加斜槓,而且還要兩次? – deceze 2012-04-18 23:03:28

+0

以及我沒有;我自己寫的代碼,但我相信它與我工作正常因爲我已經使用過一次,但從來沒有再次。但我相信它將它添加到tmp_name和名稱itslef並使用此代碼就像2年前,所以我relly不記得這麼多對不起 – 2012-04-18 23:05:56

+1

嗯,親自,我不會使用代碼,我不完全理解,更不用說在公共論壇上貼上我的名字了...... – deceze 2012-04-18 23:08:23

1

這通常是一個非常糟糕的主意,圖像直接上傳到數據庫。原因是因爲過了一段時間,從數據庫讀取和上傳文件會使數據庫過載。

更好的解決方案是將圖像上傳到服務器上的文件夾,然後將文件名和位置保存在數據庫中。