我试图使用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