当php引发错误时,如何显示文件上传输入字段旁边的错误
我有一个简单的HTML表单,该表格具有三个输入字段,其中包含type =“ file”。 PHP处理在数组中上传。一切都起作用,但我无法显示出错误时,靠近class =“ error_highlight”的“ div”中的输入字段。
我希望对脚本底部附近的两个“ else”语句的两个评论在发生错误时出现。他们以“显示此错误:”开头。有人可以帮我吗?
<?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";
}
}
}
?&gt;
<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>
I have a simple HTML form that has three input fields with type="file". The PHP processes uploads in an array. Everything works but I cannot show the error, when occurred, next to the input field in a "div" with class="error_highlight".
I want the two comments on the two "Else" statements close to the bottom of the script to show up when the error occurs. They begin with "Show this error:". Can someone please help me with this?
<?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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先,您似乎没有声明$错误,而只是循环$错误。
您需要使用错误并使用数组显示错误。喜欢
$ _文件['images'] ['错误']
First you dont seem to declare $errors but just a looping $error.
You need to Go though errors and use an array to show errors. Like Go though
$_FILES['images']['error']