当php引发错误时,如何显示文件上传输入字段旁边的错误

发布于 2025-02-04 03:44:17 字数 2412 浏览 2 评论 0原文

我有一个简单的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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

扶醉桌前 2025-02-11 03:44:17

首先,您似乎没有声明$错误,而只是循环$错误。
您需要使用错误并使用数组显示错误。喜欢$ _文件['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']

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文