[英]How to upload multiple images in php and store it as blob mysql
Here is my current code and can anyone help me how i can upload multiple images with a text in category? 这是我当前的代码,任何人都可以帮助我如何上传带有类别文本的多张图片? i have to store the images as blob in database. 我必须将图像作为Blob存储在数据库中。
<?php
if(count($_FILES) > 0) {
if(is_uploaded_file($_FILES['userImage']['tmp_name'])) {
mysql_connect("localhost", "root", "");
mysql_select_db ("au");
$cat = $_POST['cat'];
$imgData =addslashes(file_get_contents($_FILES['userImage']['tmp_name']));
$imageProperties = getimageSize($_FILES['userImage']['tmp_name']);
$sql = "INSERT INTO output_images(imageType ,imageData, category)
VALUES('{$imageProperties['mime']}', '{$imgData}', '$cat')";
$current_id = mysql_query($sql) or die("<b>Error:</b> Problem on Image Insert<br/>" . mysql_error());
if(isset($current_id)) {
header("Location: listImages.php");
}
}
}
?>
<HTML>
<HEAD>
<TITLE>Upload Image to MySQL BLOB</TITLE>
<link href="imageStyles.css" rel="stylesheet" type="text/css" />
</HEAD>
<BODY>
<form name="frmImage" enctype="multipart/form-data" action="" method="post" class="frmImageUpload">
<label>Upload Image File:</label><br/>
<input type="text" name="cat">
<input name="userImage" type="file" class="inputFile" />
<input type="submit" value="Submit" class="btnSubmit" />
</form>
</div>
</BODY>
</HTML>
I have two articles in my blog refer to multiple files uploading and storing images in MySQL database. 我的博客中有两篇文章提到了多个文件在MySQL数据库中上传和存储图像。
upload.html upload.html
<form action="product.php" method="post" enctype="multipart/form-data">
<input type="file" class="form-control-file" name="prod_pic[]" id="prod_pic[]">
<input type="file" class="form-control-file" name="prod_pic[]" id="prod_pic[]">
:
<button type="submit" class="btn btn-primary">Save Prodcut Info</button>
</form>
Note that the 'name'
property of the <input>
tag should be added an array tag '[]'
. 注意, <input>
标记的'name'
属性应该添加一个数组标记'[]'
。 In addition, enctype="multipart/form-data"
should be added in the tag in order to enable the file uploading function. 另外,应在标签中添加enctype="multipart/form-data"
,以启用文件上传功能。 Thus, the $_FILES
variable can be an array to carry multiple files information. 因此, $_FILES
变量可以是一个包含多个文件信息的数组。
Then, the product.php converts $_FILE variable and save files to the destination path. 然后,product.php转换$ _FILE变量并将文件保存到目标路径。
product.php product.php
<?php
if (isset($_FILES['prod_pic'])) {
$pics = convert_upload_file_array($_FILES['prod_pic']);
foreach ($pics as $key => $pic) {
$target = "images/{$pic['name']}";
move_uploaded_file($pic['tmp_name'], $target);
}
}
?>
$_FILES
variable as an array is not as a usual form as we think. $_FILES
变量作为数组不是我们认为的通常形式。 It is formed by using file's attribute as the key, not the index number. 它是通过使用文件的属性作为键而不是索引号来形成的。 Therefore, we need a converting function convert_upload_file_array()
. 因此,我们需要一个转换函数convert_upload_file_array()
。
function convert_upload_file_array($upload_files) {
$converted = array();
foreach ($upload_files as $attribute => $val_array) {
foreach ($val_array as $index => $value) {
$converted[$index][$attribute] = $value;
}
}
return $converted;
}
Read more: How to Upload Multiple Files in PHP 阅读更多: 如何在PHP中上传多个文件
Please refer to the following code: 请参考以下代码:
$prod_pic = $mysqli->real_escape_string(file_get_contents($_FILES['prod_pic']['tmp_name']));
$prod_pic_type = $_FILES['prod_pic']['type'];
:
:
$sql = "UPDATE products
SET prod_pic = '{$prod_pic}',
prod_pic_type = '{$prod_pic_type}'
WHERE prod_id = {$prod_id}";
$mysqli->query($sql) or die($mysqli->connect_error);
$mysqli->close();
The key part is As you can see, the key part is: 如您所见,关键部分是:
file_get_contents($_FILES['prod_pic']['tmp_name'])
and 和
$mysqli->real_escape_string(......)
Read more: Insert and Update An Image to MySQL Server Field with Blob Data Type in PHP 阅读更多: 在PHP中使用Blob数据类型将图像插入和更新到MySQL服务器字段
By using the key ideas above, your goal should be easily achieved. 通过使用上述关键思想,您的目标应该很容易实现。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.