如何使用PHP在MySQL数据库中插入图像?
我使用此表格:如何使用PHP在MySQL数据库中插入图像?,php,mysql,image,file-upload,Php,Mysql,Image,File Upload,我使用此表格: <FORM action="testimage1.php" method="post"> <div style="font:bold 10px arial,serif;" >Product Name*</div> <input type="text" name="myuserName" maxlength="50" /><br />
<FORM action="testimage1.php" method="post">
<div style="font:bold 10px arial,serif;" >Product Name*</div>
<input type="text" name="myuserName" maxlength="50" /><br />
<div style="font:bold 10px arial,serif;" >Upload a photo</div>
<input name="uploadimage" type="file" /></br>
<div style="font:bold 10px arial,serif;">Product Description:</div> <input type="text" name="product" value=""></br>
<input id="submit" type="submit" value="submit" /><br />
</form>
而错误是:-
file_get_contents(DSC02945.JPG)[function.file get contents]:打开流失败:在第22行的C:\xampp\htdocs\thinstrokes original site\testimage1.php中没有这样的文件或目录
警告:getimagesize(DSC02945.JPG)[function.getimagesize]:无法打开流:第23行的C:\xampp\htdocs\thinstrokes original site\testimage1.php中没有此类文件或目录
如何更正…?在表单声明中需要enctype=multipart/form数据。并通过$\u FILES变量而不是$\u POST变量访问该文件。比如:
<form action="testimage1.php" method="post" enctype="multipart/form-data">
<input name="uploadimage" type="file" />
</form>
<?php
$filename = $_FILES['uploadimage']['tmp_name'];
?>
enctype应为多部分/表单数据还应检查product
表中的image
属性是否为BLOB
类型(否则,由于字符集转换,二进制数据可能会混乱)为什么要在数据库中插入图像…通常我们将图像存储在文件夹中,并将图像名称存储在db中…这样会使您的db变得干净…我只想添加一个图像jpg/png,而不是您需要的文件夹$imgData
(以及其他所有内容)在将其放入查询之前,先通过mysql\u real\u escape\u string。这是另一个问题。您现在已成功上传文件,这是对当前问题的回答。请解释您更正的内容以及错误。
<form action="testimage1.php" method="post" enctype="multipart/form-data">
<input name="uploadimage" type="file" />
</form>
<?php
$filename = $_FILES['uploadimage']['tmp_name'];
?>
$imgData = file_get_contents($filename);
$size = getimagesize($filename);
mysql_connect("localhost", "$username", "$password");
mysql_select_db ("$dbname");
$sql = sprintf("INSERT INTO testblob
(image_type, image, image_size, image_name)
VALUES
('%s', '%s', '%d', '%s')",
mysql_real_escape_string($size['mime']),
mysql_real_escape_string($imgData),
$size[3],
mysql_real_escape_string($_FILES['userfile']['name'])
);
mysql_query($sql);