多个PHP图像上载,仅上载选定的第一个图像
我已经建立了一个多文件上传表单。但是,当我选择多个图像并单击“上载”时,只有第一个图像上载,而不是图像数组?干杯多个PHP图像上载,仅上载选定的第一个图像,php,file,upload,Php,File,Upload,我已经建立了一个多文件上传表单。但是,当我选择多个图像并单击“上载”时,只有第一个图像上载,而不是图像数组?干杯 <?php // Check if the form was submitted if($_SERVER["REQUEST_METHOD"] == "POST"){ // Check if file was uploaded without errors if(isset($_FILES["photo"]) && $_FILES["photo"]
<?php
// Check if the form was submitted
if($_SERVER["REQUEST_METHOD"] == "POST"){
// Check if file was uploaded without errors
if(isset($_FILES["photo"]) && $_FILES["photo"]["error"] == 0){
$allowed = array("jpg" => "image/jpg", "jpeg" => "image/jpeg", "gif" => "image/gif", "png" => "image/png");
$filename = $_FILES["photo"]["name"];
$filetype = $_FILES["photo"]["type"];
$filesize = $_FILES["photo"]["size"];
// Verify file extension
$ext = pathinfo($filename, PATHINFO_EXTENSION);
if(!array_key_exists($ext, $allowed)) die("Error: Please select a valid file format.");
// Verify file size - 20MB maximum
$maxsize = 20 * 1024 * 1024;
if($filesize > $maxsize) die("Error: File size is larger than the allowed limit.");
// Verify MYME type of the file
if(in_array($filetype, $allowed)){
// Check whether file exists before uploading it
if(file_exists("upload/" . $filename)){
echo $filename . " is already exists.";
} else{
move_uploaded_file($_FILES["photo"]["tmp_name"], "upload/" . $filename);
echo "Your file was uploaded successfully.";
}
} else{
echo "Error: There was a problem uploading your file. Please try again.";
}
} else{
echo "Error: " . $_FILES["photo"]["error"];
}
} ?>
以下是您需要执行的操作:
- 输入名称必须定义为数组,即name=“inputName[]”
- 输入元素必须有multiple=“multiple”或只有multiple
- 在PHP文件中使用语法“$\u FILES['inputName']['param'][index]”
- 请确保查找空文件名和路径,数组可能包含空字符串。在计数之前使用数组_filter()
- 下面是一个糟糕的示例(仅显示相关代码)
PHP:
/$files=array_filter($_files['upload']['name'])//在处理文件之前要使用类似的东西。
//阵列中上载文件的计数#
$total=计数($_文件['upload']['name']);
//循环浏览每个文件
对于($i=0;$i<$total;$i++){
//获取临时文件路径
$tmpFilePath=$\u文件['upload']['tmp\u name'][$i];
//确保我们有一个文件路径
如果($tmpFilePath!=“”){
//设置我们的新文件路径
$newFilePath=“./uploadFiles/”$_FILES['upload']['name'][$i];
//将文件上载到临时目录
if(移动上传的文件($tmpFilePath,$newFilePath)){
//在这里处理其他代码
}
}
}
希望这有帮助 我对您的代码做了一些更改。请使用它。我可以帮助您,也请检查
<input type="file" multiple>
if(isset($_FILES["photo"]) && $_FILES["photo"]["error"] == 0){
$allowed = array("jpg" => "image/jpg", "jpeg" => "image/jpeg", "gif" => "image/gif", "png" => "image/png");
$countfiles = count($_FILES['photo']['name']);
for($i=0;$i<$countfiles;$i++){
$filename = $_FILES["photo"]["name"][$i];
$filetype = $_FILES["photo"]["type"][$i];
$filesize = $_FILES["photo"]["size"][$i];
// Verify file extension
$ext = pathinfo($filename, PATHINFO_EXTENSION);
if(!array_key_exists($ext, $allowed)) die("Error: Please select a valid file format.");
// Verify file size - 20MB maximum
$maxsize = 20 * 1024 * 1024;
if($filesize > $maxsize) die("Error: File size is larger than the allowed limit.");
// Verify MYME type of the file
if(in_array($filetype, $allowed)){
// Check whether file exists before uploading it
if(file_exists("upload/" . $filename)){
echo $filename . " is already exists.";
} else{
move_uploaded_file($_FILES["photo"]["tmp_name"][$i], "upload/" . $filename);
echo "Your file was uploaded successfully.";
}
} else{
echo "Error: There was a problem uploading your file. Please try again.";
}
}
} else{
echo "Error: " . $_FILES["photo"]["error"];
}
如果(isset($\u文件[“照片”])&&&$\u文件[“照片”][“错误”]==0){
$allowed=array(“jpg”=>“image/jpg”、“jpeg”=>“image/jpeg”、“gif”=>“image/gif”、“png”=>“image/png”);
$countfiles=count($_文件['photo']['name']);
对于($i=0;$i$maxsize)模具(“错误:文件大小大于允许的限制”);
//验证文件的MYME类型
if(在数组中($filetype,$allowed)){
//上传文件前检查文件是否存在
如果(文件_存在(“上传/”$filename)){
echo$filename。“已存在。”;
}否则{
移动上传的文件($\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\;
echo“您的文件已成功上载。”;
}
}否则{
echo“错误:上载文件时出现问题。请重试。”;
}
}
}否则{
echo“Error:”.$\u文件[“photo”][“Error”];
}
}?>因为您的代码不会在文件arrayokay上迭代。。我该怎么做…你必须给上传一个for循环。。这一个不行。文件名也应该以数组的形式给出。OP为什么要使用它?好的答案总是有一个解释,说明做了什么以及为什么这样做,不仅是为了OP,而且是为了未来的访客,以便他们可以找到这个问题并阅读你的答案。如果答案很好,他们可能会投赞成票。
//$files = array_filter($_FILES['upload']['name']); //something like that to be used before processing files.
// Count # of uploaded files in array
$total = count($_FILES['upload']['name']);
// Loop through each file
for( $i=0 ; $i < $total ; $i++ ) {
//Get the temp file path
$tmpFilePath = $_FILES['upload']['tmp_name'][$i];
//Make sure we have a file path
if ($tmpFilePath != ""){
//Setup our new file path
$newFilePath = "./uploadFiles/" . $_FILES['upload']['name'][$i];
//Upload the file into the temp dir
if(move_uploaded_file($tmpFilePath, $newFilePath)) {
//Handle other code here
}
}
}
<input type="file" multiple>
if(isset($_FILES["photo"]) && $_FILES["photo"]["error"] == 0){
$allowed = array("jpg" => "image/jpg", "jpeg" => "image/jpeg", "gif" => "image/gif", "png" => "image/png");
$countfiles = count($_FILES['photo']['name']);
for($i=0;$i<$countfiles;$i++){
$filename = $_FILES["photo"]["name"][$i];
$filetype = $_FILES["photo"]["type"][$i];
$filesize = $_FILES["photo"]["size"][$i];
// Verify file extension
$ext = pathinfo($filename, PATHINFO_EXTENSION);
if(!array_key_exists($ext, $allowed)) die("Error: Please select a valid file format.");
// Verify file size - 20MB maximum
$maxsize = 20 * 1024 * 1024;
if($filesize > $maxsize) die("Error: File size is larger than the allowed limit.");
// Verify MYME type of the file
if(in_array($filetype, $allowed)){
// Check whether file exists before uploading it
if(file_exists("upload/" . $filename)){
echo $filename . " is already exists.";
} else{
move_uploaded_file($_FILES["photo"]["tmp_name"][$i], "upload/" . $filename);
echo "Your file was uploaded successfully.";
}
} else{
echo "Error: There was a problem uploading your file. Please try again.";
}
}
} else{
echo "Error: " . $_FILES["photo"]["error"];
}