使用PHP上传多个图像
我有下面的代码,我似乎不明白为什么我的查询失败了。有人知道为什么吗?使用PHP上传多个图像,php,html,mysql,Php,Html,Mysql,我有下面的代码,我似乎不明白为什么我的查询失败了。有人知道为什么吗? 我在网上找到了许多建议的解决方案,但都没有奏效 HTML表单代码遵循PHP代码 <?php include 'connection.php';?> <?php $dir = substr(uniqid(),-7); $valid_formats = array("jpg", "png", "gif", "jpeg"); $max_file_size = 1024*
我在网上找到了许多建议的解决方案,但都没有奏效 HTML表单代码遵循PHP代码
<?php include 'connection.php';?>
<?php
$dir = substr(uniqid(),-7);
$valid_formats = array("jpg", "png", "gif", "jpeg");
$max_file_size = 1024*100; //100 kb
/*
$path = "Prototype/uploads/"; // Upload directory
mkdir ($path, 0744);\
*/
$count = 0;
if (isset($_POST['search'])) {
// Loop $_FILES to exeicute all files
foreach ($_FILES['files']['name'] as $f => $name) {
echo "$name--";
if ($_FILES['files']['error'][$f] == 4) {
continue; // Skip file if any error found
echo "something <br>";
}
if ($_FILES['files']['error'][$f] == 0) {
if ($_FILES['files']['size'][$f] > $max_file_size) {
$message[] = "$name is too large!.";
echo "something***************** <br>";
continue; // Skip large files
} elseif (! in_array(pathinfo($name, PATHINFO_EXTENSION), $valid_formats)) {
$message[] = "$name is not a valid format";
echo "something+++++++++++++++++++ <br>";
echo "$name-- ";
continue; // Skip invalid file formats
} else { // No error found! Move uploaded files
// if(move_uploaded_file($_FILES["files"]["tmp_name"][$f], $path.$name))
// $count=$count+1; // Number of successfully uploaded file
//echo $path.$name;
$image = addslashes(file_get_contents($_FILES['files']['tmp_name'][$f]));
$image_name = addslashes($_FILES['files']['name'][$f]);
$query2 = "Insert into $dbname.Image (Image, ImageName) VALUES ('$image', '$image_name')";
$result2 = mysqli_query($conn,$query2);
if (!$result2) {
echo "ERRORS";
}
//Number of successfully uploaded file
}
}
}
echo "$count files were imported";
}
//show success message
/*
echo "<h1>Uploaded:</h1>";
if(is_array($files)){
echo "<ul>";
foreach($files as $file){
echo "<li>$file</li>";
}
echo "</ul>";
}
*/
?>
<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post" enctype="multipart/form-data">
<label class="btn btn-primary" for="my-file-selector">
<input id="my-file-selector" type="file" name="files[]" style="display:none;" multiple onchange="$('#upload-file-info').html($(this).val());">
Browse
</label>
<span class='label label-info' id="upload-file-info"></span>
<div style="float:right;">
<label class="btn btn-primary" for="my-file-selector2">
<input id="my-file-selector2" type="Submit" style="display:none;" name="search">
Save
</label>
</div>
</form>
我建议您不要将图像直接保存到数据库中
相反,您可以将文件移动到像S3这样的云服务,并将该url存储在数据库中
如果S3不是您的选择,请将图像上载到同一项目中的任何文件夹并存储该url
在数据库中存储二进制图像有什么具体原因吗?我建议您不要直接将图像保存到数据库中
相反,您可以将文件移动到像S3这样的云服务,并将该url存储在数据库中
如果S3不是您的选择,请将图像上载到同一项目中的任何文件夹并存储该url
在数据库中存储二进制图像有什么特别的原因吗?试试以下方法:-
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Multiple File Ppload with PHP</title>
</head>
<body>
<form action="" method="post" enctype="multipart/form-data">
<input type="file" id="file" name="files[]" multiple="multiple" accept="image/*" />
<input type="submit" value="Upload!" />
</form>
</body>
</html>
链接:-试试这个:-
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Multiple File Ppload with PHP</title>
</head>
<body>
<form action="" method="post" enctype="multipart/form-data">
<input type="file" id="file" name="files[]" multiple="multiple" accept="image/*" />
<input type="submit" value="Upload!" />
</form>
</body>
</html>
链接:-您面临什么错误?如果(!$result2)
失败,则此条件,因此我假设有问题。打印$\u文件数组并检查其返回属性(如果听起来很愚蠢)。您是否要求我打印变量$name
(foreach($\u FILES['FILES']['name']as$f=>$name)
)?@vighaneshmandavkar感谢链接,但他们正在保存图像路径,但在我这边,我存储实际图像(longblob
类型)。\n您面临什么错误?此条件如果(!$result2)
失败,因此我假设有问题。请打印$\u文件数组,如果听起来很愚蠢,请检查其返回属性。您是否要求我打印变量$name
(foreach($\u FILES['FILES']['name']as$f=>$name)
)?@vighaneshmandavkar感谢您的链接,但他们正在保存图像的路径,但在我这边,我存储实际的图像(longblob
类型)\