我有下面的代码,我似乎无法弄清楚为什么我的查询失败了。有谁知道为什么? 我尝试过很多在网上找到的建议解决方案,但都没有。
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>
答案 0 :(得分:0)
我建议你不要将图像直接保存到数据库中。
相反,您可以将文件移动到像S3这样的云服务,并将该URL存储在数据库中。
如果您不选择S3,请将图片上传到同一项目中的任何文件夹并存储该网址。
将二进制图像存储在数据库中是否有任何特定原因?
答案 1 :(得分:0)
试试这个: -
<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>
$valid_formats = array("jpg", "png", "gif", "zip", "bmp");
$max_file_size = 1024*100; //100 kb
$path = "uploads/"; // Upload directory
$count = 0;
if(isset($_POST) and $_SERVER['REQUEST_METHOD'] == "POST"){
// Loop $_FILES to exeicute all files
foreach ($_FILES['files']['name'] as $f => $name) {
if ($_FILES['files']['error'][$f] == 4) {
continue; // Skip file if any error found
}
if ($_FILES['files']['error'][$f] == 0) {
if ($_FILES['files']['size'][$f] > $max_file_size) {
$message[] = "$name is too large!.";
continue; // Skip large files
}
elseif( ! in_array(pathinfo($name, PATHINFO_EXTENSION), $valid_formats) ){
$message[] = "$name is not a valid format";
continue; // Skip invalid file formats
}
else{ // No error found! Move uploaded files
if(move_uploaded_file($_FILES["files"]["tmp_name"][$f], $path.$name))
$count++; // Number of successfully uploaded file
}
}
}
}