我正在尝试使用PHP(Mysqli)将图像插入到mysql数据库服务器端和客户端上的ajax但是当我提交图像时 它显示如下错误
警告:文件上传错误 - 无法在第0行的“未知”中创建临时文件
警告:file_get_contents():第3行的x:\ xxxx \ xxxx \ xxxx \ xxxx \ insert.php中的文件名不能为空
AJAX:
$("form[name='frmImage']").submit(function(e) {
var formData = new FormData($(this)[0]);
$.ajax({
url: "insert.php",
type: "POST",
data: formData,
async: false,
success: function (msg) {
alert(msg);
$('#error').html(msg);
},
cache: false,
contentType: false,
processData: false
});
e.preventDefault();
});
HTML:
<form name="frmImage" method="post" action="" enctype="multipart/form-data" >
<input name="userImage" type="file" class="inputFile" />
<input type="submit" value="Submit" class="btnSubmit" />
</form>
<div id="error">
PHP:
$db = mysqli_connect("xxx","xxx","xxxx","xxx"); //keep your db name
$image = addslashes(file_get_contents($_FILES['userImage']['tmp_name']));
$query = "INSERT INTO images (id,image) VALUES('','$image')";
$qry = mysqli_query($db, $query);
Sql Query:
CREATE TABLE `images` (
`id` int(100) NOT NULL AUTO_INCREMENT,
`image` longblob,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
答案 0 :(得分:0)
您在php代码中错过了字段名称的匹配错误:您的文件输入名称为userImage
,因此您可以使用userImage
获取图片属性
更改强>
$_FILES['formData']['tmp_name']
要强>
$_FILES['userImage']['tmp_name']