当PHP抛出错误时,如何在文件上传输入字段旁边显示错误



我有一个简单的HTML表单,它有三个输入字段,type="file"PHP以数组的形式处理上传。一切正常,但我无法显示错误,当发生时,在输入字段旁边的"div"类="error_highlight".

我想要两个"Else"语句靠近脚本底部,以便在发生错误时显示。它们以&quot开头,显示这个错误:"。有人能帮我一下吗?

<?php
if(!empty($_FILES['images'])){
// File upload configuration
$allowTypes = array('jpg','png','jpeg','gif');

$images_arr = array();
foreach($_FILES['images']['name'] as $key=>$val){
$image_name = $_FILES['images']['name'][$key];
$tmp_name   = $_FILES['images']['tmp_name'][$key];
$size       = $_FILES['images']['size'][$key];
$type       = $_FILES['images']['type'][$key];
$error      = $_FILES['images']['error'][$key];

$fileType = pathinfo($image_name);// split path in its components 
$extension = strtolower($fileType['extension']);// extract the extension and normalize to lowercase
if(in_array($extension, $allowTypes)){
echo $extension;
if($error === 0){
$fileNameNew = uniqid('', true).".".$extension;
$fileDestination = 'user_images/'.$fileNameNew;
move_uploaded_file($tmp_name, $fileDestination);
//header("location: test");
} else {
// Show this error as $errors = "An error prevented from uploading your image / file. Please try again.";
}
} else {
// Show this error as $errors = "Unable to upload the selected file type. Please use files or images with extensions ending on jpg / jpeg / png / pdf";
}
}
}

?比;

<form action="" method="post" enctype="multipart/form-data">
<div>
<input type="file"  name="images[]" >
<div class="error_highlight"><?php echo $errors; ?></div>
</div>
<div>
<input type="file"  name="images[]" >
<div class="error_highlight"><?php echo $errors; ?></div>
</div>  
<div>
<input type="file"  name="images[]" >
<div class="error_highlight"><?php echo $errors; ?></div>
</div>
<input type="submit" name="submit" value="Upload all files">
</form>

首先你似乎没有声明$errors,而只是一个循环的$error。您需要浏览错误并使用数组来显示错误。像Go through$_FILES['images']['error']

最新更新