我試圖使用php和mysqli將圖像上傳到數據庫中作爲blob。如果我只是嘗試使用查詢來插入圖像名稱,它會工作得很好,並創建一個名稱爲新的行(blob爲空)。一旦我開始嘗試使用查詢和jQuery函數通過表單將數據上傳到數據庫中,就會創建一個新行,但該數據塊顯示0 B。代碼的第一位是html表單。第二個是提交表單後調用的php文件。任何建議,將不勝感激。提前致謝。PHP和mysqli:使用表格將blob上傳到數據庫
澄清:據我所知,上傳圖像到目錄是一種更有效的方式來存儲它。爲了理解如何使用其他選項,我試圖弄清楚如何使用blob工作。
編輯
只是爲了澄清我也試過下面的代碼片段。
if ($filetype == "image/jpeg" && $filesize > 0 && $filesize < 1048576) {
echo "Import of photo success";
$aimage = file_get_contents($tmpfile);
if (!($stmt=$mysqli->prepare("INSERT INTO actor_info (aname, aimage) VALUE (?,?)"))) {
echo "Prepare failed: (" . $mysqli->errno . ")" . $mysqli->error;
}
if (!$stmt->bind_param("sb", $_POST['aname'], $aimage)) {
echo "Binding parameters failed: (" . $stmt->errno .") " . $stmt->error;
}
if (!$stmt->execute()) {
echo "Execute failed: (" . $stmt->errno . ") " . $stmt->error;
}
else {
printf("%d row inserted.<br/>", $stmt->affected_rows);
}
}
編輯完
錯誤消息導入照片的成功
注意:未定義指數:aimage中/ NFS/STAK /學生/ M/martalic /的public_html/CS494 /第43行的Test/addActorInfo.php
警告:file_get_contents()[function.file-get-contents]:文件名不能在/ nfs/stak/stude中爲空NTS/M/martalic /的public_html/CS494 /測試/ addActorInfo.php上線43 1行插入
HTML表單
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<form id="form" method="post" action=addActorInfo.php enctype="multipart/form-data">
Actor Name: <input id="aname" type="text" name="aname" class="required" maxlength="64"><br><br>
Attach Photo: <input id="aimage" type="file" name="aimage" class="required"><br><br>
<input type="submit" name="ADD" value="ADD"/>
</form>
</body>
</html>
PHP
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
Actor Information
<?php
ini_set('display_errors', 'On');
ini_set('display_startup_errors', 'On');
error_reporting(E_ALL);
$mysqli = new mysqli($dbhost, $dbuser, $dbpass, $dbname);
if ($mysqli->connect_errno) {
echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
}
if(isset($_POST["ADD"])) {
$aname = $_POST['aname'];
$errorinfo = $_FILES["aimage"]["error"];
$filename = $_FILES["aimage"]["name"];
$tmpfile = $_FILES["aimage"]["tmp_name"];
$filesize = $_FILES["aimage"]["size"];
$filetype = $_FILES["aimage"]["type"];
if (!($filetype == "image/jpeg" && $filesize > 0)) {
echo "Import of photo failed";
}
if ($filetype == "image/jpeg" && $filesize > 0 && $filesize < 1048576) {
echo "Import of photo success";
if (!($stmt=$mysqli->prepare("INSERT INTO actor_info (aname, aimage) VALUE (?,?)"))) {
echo "Prepare failed: (" . $mysqli->errno . ")" . $mysqli->error;
}
$null = NULL;
if (!$stmt->bind_param("sb", $_POST['aname'], $null)) {
echo "Binding parameters failed: (" . $stmt->errno .") " . $stmt->error;
}
if (!$stmt->send_long_data(0, file_get_contents($_POST['aimage']))) {
echo "Did not get contents";
}
if (!$stmt->execute()) {
echo "Execute failed: (" . $stmt->errno . ") " . $stmt->error;
}
else {
printf("%d row inserted.<br/>", $stmt->affected_rows);
}
}
else {
echo "Image must be under 1 MB";
}
$stmt->close();
}
$mysqli->close();
?>
</body>
</html>
絕對。我正在嘗試使用blob來獲取它的工作方式。 – user1852050 2013-02-17 08:07:53
不要忘記BLOB的東西,看起來你的代碼是正確的上傳,只是注意你上傳它的位置,並將文件的名稱作爲VARCHAR放入數據庫。不要讓BLOB讓你失望。 – JonE 2013-02-17 08:24:07
感謝您的建議,但我有興趣瞭解如何blob工作上傳和存儲。我可能會也可能不會再使用它,但至少我會理解它的優點和缺點,因爲我已經嘗試過了。 – user1852050 2013-02-17 08:28:04