我正在尝试上传3个图片字段,将其存储在我的数据库中,然后将上传的文件移到文件夹中。
目前,“image1”,“image2”,“image3”,“image4”字段正在插入MySQL数据库,但只有“image1”正在上传并移动到我的文件夹中。
这是我的代码:
if(isset($_POST['submit'])){
$pro_image1 = $_FILES['image1']['name'];
$pro_image2 = $_FILES['image2']['name'];
$pro_image3 = $_FILES['image3']['name'];
$pro_image4 = $_FILES['image4']['name'];
$tmp_name = $_FILES['image1']['tmp_name'];
$tmp2_name = $_FILES['image2']['tmp_name'];
$tmp3_name = $_FILES['image3']['tmp_name'];
$tmp4_name = $_FILES['image4']['tmp_name'];
$pro_query = "INSERT INTO product(image1,image2,image3,image4) VALUES('$pro_image1','$pro_image2','$pro_image3','$pro_image4')";
if(mysqli_query($con,$pro_query)){
$msg = "<p class='pull-right' style='color:green;'> Product Added successfully</p>";
$path = "images/$pro_image1";
if(move_uploaded_file($tmp_name, $path)) {
copy($path, "../$path");
}
if(move_uploaded_file($tmp2_name, $path)) {
copy($path, "../$path");
}
if(move_uploaded_file($tmp3_name, $path)) {
copy($path, "../$path");
}
if(move_uploaded_file($tmp4_name, $path)) {
copy($path, "../$path");
} elseif(!mysqli_query($con,$pro_query)) {
$insert_error = "<p class='pull-right' style='color:red;>Product didn't added</p>";
}
}
}
<form action="" method="post" enctype="multipart/form-data" class="form-font">
<div class="col-md-6">
<div class="form-group">
<label>Image1</label>
<input type="file" name="image1" class="form-control" required>
</div>
<div class="form-group">
<label>Image2</label>
<input type="file" name="image2" class="form-control" required>
</div>
<div class="form-group">
<label>Image3</label>
<input type="file" name="image3" class="form-control" required>
</div>
<div class="form-group">
<label>Image4</label>
<input type="file" name="image4" class="form-control" required>
</div>
<center>
<input type='submit' name='submit' class='btn btn-success' value='Add Product'>
</center>
</div>
</form>
如何在我的文件夹中移动其他图像(“image2”,“image3”,“image4”)?
我在代码中犯了什么错误?你能解释一下我在哪里做错了吗?
答案 0 :(得分:0)
您只为image1定义了var $ path - 您需要在每个&#34;中更改此var;如果&#34;阻止函数move_uploaded_file和复制。 目前,您只为所有图像使用image1的路径和文件名。
答案 1 :(得分:0)
你只需将它们命名为&#34;图像[0],图像[1],图像[2]和#34; 代码中的更改
<form action="" method="post" enctype="multipart/form-data" class="form-font">
<div class="col-md-6">
<div class="form-group">
<label>Image1</label>
<input type="file" name="image[0]" class="form-control" required>
</div>
<div class="form-group">
<label>Image2</label>
<input type="file" name="image[1]" class="form-control" required>
</div>
<div class="form-group">
<label>Image3</label>
<input type="file" name="image[2]" class="form-control" required>
</div>
<div class="form-group">
<label>Image4</label>
<input type="file" name="image[3]" class="form-control" required>
</div>
<center><input type='submit' name='submit' class='btn btn-success' value='Add Product'></center>
</form>
答案 2 :(得分:0)
请覆盖您的$ path变量,然后它将上传属性..
之前如果条件改变$ path varibale如下或将变量更改为$ path1,$ path2,$ path3 ..
$path = "images/$pro_image1";
$path = "images/$pro_image2";
$path = "images/$pro_image3";
-
if(isset($_POST['submit'])){
$pro_image1 = $_FILES['image1']['name'];
$pro_image2 = $_FILES['image2']['name'];
$pro_image3 = $_FILES['image3']['name'];
$pro_image4 = $_FILES['image4']['name'];
$tmp_name = $_FILES['image1']['tmp_name'];
$tmp2_name = $_FILES['image2']['tmp_name'];
$tmp3_name = $_FILES['image3']['tmp_name'];
$tmp4_name = $_FILES['image4']['tmp_name'];
$pro_query = "INSERT INTO product(image1,image2,image3,image4) VALUES('$pro_image1','$pro_image2','$pro_image3','$pro_image4')";
if(mysqli_query($con,$pro_query)){
$msg = "<p class='pull-right' style='color:green;'> Product Added successfully</p>";
$path = "images/$pro_image1";
if(move_uploaded_file($tmp_name, $path)){
copy($path, "../$path");
}
$path = "images/$pro_image2";
if(move_uploaded_file($tmp2_name, $path)){
copy($path, "../$path");
}
$path = "images/$pro_image3";
if(move_uploaded_file($tmp3_name, $path)){
copy($path, "../$path");
}
$path = "images/$pro_image4";
if(move_uploaded_file($tmp4_name, $path)){
copy($path, "../$path");
}
elseif(!mysqli_query($con,$pro_query)){
$insert_error = "<p class='pull-right' style='color:red;>Product didn't added</p>";
}
}
}
<form action="" method="post" enctype="multipart/form-data" class="form-font">
<div class="col-md-6">
<div class="form-group">
<label>Image1</label>
<input type="file" name="image1" class="form-control" required>
</div>
<div class="form-group">
<label>Image2</label>
<input type="file" name="image2" class="form-control" required>
</div>
<div class="form-group">
<label>Image3</label>
<input type="file" name="image3" class="form-control" required>
</div>
<div class="form-group">
<label>Image4</label>
<input type="file" name="image4" class="form-control" required>
</div>
<center><input type='submit' name='submit' class='btn btn-success' value='Add Product'></center>
</form>
答案 3 :(得分:0)
以下是您需要做的事情:
<script type=“text/javascript”>
function uploadProgressHandler(event)
{
$("#loaded_n_total").html("Uploaded "+event.loaded+" bytes of "+event.total);
var percent = (event.loaded / event.total) * 100;
var progress = Math.round(percent);
$("#uploadProgressBar").html(progress + " percent na ang progress");
$("#uploadProgressBar").css("width", progress + "%");
$("#status").html(progress +"% uploaded... please wait");
}
function loadHandler(event)
{
$("#status").html(event.target.responseText);
$("#uploadProgressBar").css("width", "0%");
}
function errorHandler(event){
$("#status").html("Upload Failed");
}
function abortHandler(event){
$("#status").html("Upload Aborted");
}
$("#uploadFile").click(function(event)
{
event.preventDefault();
var file = $("#fileUpload")[0].files[0];
var formData = new FormData();
formData.append("file1", file);
$.ajax({url: 'http://testarea.local/UploadWithProgressBar1/file_upload_parser.php',
method: 'POST',
type: 'POST',
data: formData,
contentType: false,
processData: false,
xhr: function()
{
var xhr = new window.XMLHttpRequest();
xhr.upload.addEventListener("progress",
uploadProgressHandler,
false
);
xhr.addEventListener("load", loadHandler, false);
xhr.addEventListener("error", errorHandler, false);
xhr.addEventListener("abort", abortHandler, false);
return xhr;
}
}
);
}
);
</script>