我目前有一段時間上傳圖像到我的數據庫。我目前有多個變量/輸入從一個表單上傳 - 如果這些輸入是圖像文件上傳的話。該文件似乎使它做數據庫,但是當我嘗試通過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
爲什麼要將圖像存儲在數據庫中,而不是將文件保存到目錄並將文件的路徑保存到數據庫中,是否有特殊原因?保存文件路徑對於數據庫來說會比存儲整個圖像好得多。 – knittledan 2012-04-18 22:55:00
這首先是因爲'$ _FILES ['photo']'*是一個包含有關上傳文件信息的數組。這不是上傳的文件本身。你有沒有嘗試[查閱手冊](http://php.net/manual/en/features.file-upload.php)關於文件上傳? – deceze 2012-04-18 22:56:29
@knittledan我相信BLOB數據類型存儲在表格本身之外,並且表格只包含一個指向信息的指針。因此,將數據存儲在數據庫中不會導致普通查詢出現問題(儘管它可能會減慢備份和複製速度)。 – octern 2012-04-18 22:58:37