我正在尝试使用 PHP 和 mysqli 将图像作为 blob 上传到数据库中。如果我只是尝试使用查询插入图像名称,它就可以正常工作并创建一个具有该名称的新行(blob 为空)。一旦我开始尝试使用查询和 jquery 函数通过表单将 blob 上传到数据库中,就会创建一个新行,但 blob 显示 0 B。代码的第一位是 html 表单。第二个是提交表单后调用的 php 文件。任何建议,将不胜感激。提前致谢。
澄清:我知道将图像上传到目录是一种更有效的存储方式。为了了解如何使用其他选项,我试图弄清楚使用 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 in /nfs/stak/students/m/martalic/public_html/CS494/Test/addActorInfo.php on line 43
警告:file_get_contents() [function.file-get-contents]:文件名在 /nfs/stak/students/m/martalic/public_html/CS494/Test/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>