[英]Upload two different files in the same form
The issue here is, when I Upload videos, it works perfectly but not images which is giving me a hard time to figure out what the error may be这里的问题是,当我上传视频时,它可以正常工作,但不能正常工作,这让我很难弄清楚错误可能是什么
<form action="index.php" method="post" enctype="multipart/form-data" >
<h1>Browse for file to upload:</h1>
</div>
<div>
<label for="file"><h1><b>Select Video</b></h1></label>
<input type="file" name="fileToUpload1[]" id="fileToUpload" >
<br><br>
<label for="file"><h2><b>Select Image</b></h2></label>
<input type="file" name="fileToUpload1[]" id="fileToUpload" >
<br><br>
<input type="submit" value="Upload" name="submit">
</form>
The PHP Script looks like this: PHP 脚本如下所示:
$error = null;
$success = null;
$info = [];
$uploadOk = 1;
$allowedMimes = ['video/mp4', 'image/jpg'];
if(isset($_POST["submit"])) {
$counter = 0;
foreach ( $_FILES['fileToUpload1']['name'] as $file ):
$target_file = UPLOAD_DIR . basename($file);
if ( in_array(mime_content_type($_FILES['fileToUpload1']["tmp_name"][$counter]), $allowedMimes) ){
if (move_uploaded_file($_FILES['fileToUpload1']["tmp_name"][$counter], $target_file)) {
$info[] = "File ".++$counter."($file) uploaded successfully";
}else {
$info[] = "File ".++$counter."($file) cannot be uploaded";
}
} else {
$info[] = "File ".++$counter. " is not allowed.";
}
endforeach;
} else {
echo "upload a file.";
}
?>
<ul>
<?php foreach($info as $i){ ?>
<li><?=$i;?></li>
<?php }?>
</ul>
<? } ?>
The mimetype of jpg
files is actually image/jpeg
(and not iamge/jpg
), so if you are trying to upload jpg
files you should change your $allowedMimes
variable to: jpg
文件的 mimetype 实际上是image/jpeg
(而不是iamge/jpg
),因此如果您尝试上传jpg
文件,您应该将$allowedMimes
变量更改为:
$allowedMimes = ['video/mp4', 'image/jpeg'];
Also - if you want to support other image types you can use:此外 - 如果您想支持其他图像类型,您可以使用:
$allowedMimes = ['video/mp4', 'image/png', 'image/jpeg', 'image/gif', 'image/bmp'];
Another option - if you want to support all image/audio/video types you can check only the first part of the string:另一种选择 - 如果您想支持所有图像/音频/视频类型,您可以只检查字符串的第一部分:
$allowedTypes = ['video', 'image', 'audio'];
...
if ( in_array(
array_shift(
explode("/",
mime_content_type($_FILES['fileToUpload1']["tmp_name"][$counter])
)
),
$allowedTypes) )
If you are using php>=5.4 you can use:如果您使用的是 php>=5.4,则可以使用:
$allowedTypes = ['video', 'image', 'audio'];
...
if ( in_array(
explode("/",
mime_content_type($_FILES['fileToUpload1']["tmp_name"][$counter])
)[0],
$allowedTypes) )
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.