我想准备5张照片上传表格,用户需要一次选择所有5张照片并提交表格 然后我需要通过将图像重命名为" 1.jpg,2.jpg,3.jpg,4.jpg,5.jpg"来将所有5张照片存储在目标目录中。我只存储了一个图像,但在添加For循环以保存多个文件后它无法工作....请支持。
//图片上传表格
<form action="saveinntion.php" method="post" enctype="multipart/form-data">
<h1>Upload Your Innovation</h1
<fieldset>
<legend><span class="number">4</span>Upload Images</legend>
<input type="file" name="img1" id="img1" >
</br>
<input type="file" name="img2" id="img2">
</br>
<input type="file" name="img3" id="img3">
</br>
<input type="file" name="img4" id="img4">
</br>
<input type="file" name="img5" id="img5">
</br>
</fieldset>
<button type="submit">Submit</button>
</form>
saveinntion.php文件
<?php
include("dbconnection.php");
$target_dir = "Upload/";
$img=$_POST['img'];
for ($i = 0; $i < 5; $i++) {
$target_file = $target_dir . basename($_FILES['$img[]']["name"]);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
if (file_exists($target_file)) {
echo "Sorry, file already exists.";
$uploadOk = 0;
}
if ($_FILES['$img[]']["size"] > 500000) {
echo "Sorry, your file is too large.";
$uploadOk = 0;
}
if($imageFileType != "jpg" && $imageFileType != "jpeg" ) {
echo "Sorry, only JPG & JPEG files are allowed.";
$uploadOk = 0;
}
if ($uploadOk == 0) {
echo "Sorry, your file was not uploaded.";
} else {
if (move_uploaded_file($_FILES['$img[]']["tmp_name"], $target_file)) {
echo "The file ". basename( $_FILES['$img[]']["name"]). " has been uploaded.";
} else {
echo "Sorry, there was an error uploading your file.";
}
}
}
header("Location: upload1.php?id=$msg");
?>
答案 0 :(得分:2)
您的问题与您的命名有关。正如@Twinfriends在命令中所说,它不太可能上传任何文件
在代码中用$ _FILES ['img'。($ i + 1)]替换 $ _ FILES ['$ img []'] 。
第5行
$target_file = $target_dir . basename($_FILES['img'.($i+1)]["name"]);
第13行
if ($_FILES['img'.($i+1)]["size"] > 500000) {
第25行
if (move_uploaded_file($_FILES['img'.($i+1)]["tmp_name"], $target_file)) {
echo "The file ". basename( $_FILES['img'.($i+1)]["name"]). " has been uploaded.";
重命名文件。
$imageFileType = pathinfo($_FILES['img'.($i+1)]["name"],PATHINFO_EXTENSION);
$target_file = $target_dir . ($i+1).".".$imageFileType;