【问题标题】:How to check for error file input before sending them through post如何在通过帖子发送之前检查错误文件输入
【发布时间】:2019-07-14 22:30:54
【问题描述】:

在网页中,我有 2 个选项卡和 1 个提交按钮。我首先使用 type=text 制表符输入。在 type=file 的第二个输入中。我想用 1 个提交发送两个标签。但问题不在这里。在第二个选项卡中,我需要在发送文件之前检查文件是否有错误。因此,如果出现错误,则提交按钮无效。我怎样才能做到这一点? 现在我只在发送它们后检查它们是否有错误。当某些文件出现错误时,上传失败并返回错误页面。并且所有其他没有错误的文件从输入中消失。 第一个标签 http://prntscr.com/mnzc79 。第二个标签http://prntscr.com/mnzca3。 这里所有的输入都是大学学生的文件。所以我可以只使用多上传。因为它们都是不同的文件。我无法理解通过错误检查上传许多文件输入的机制。 这是我的代码

            Check if text inputs uploaded. If files were set then I push them to the array to use in query.

            if(!empty($_POST["first_name"])){
                $keys=$keys."first_name,";
                $first_name=$_POST["first_name"];
                $values=$values."'".$_POST['first_name']."',";
            }
            if(!empty($_POST["last_name"])){
                $keys=$keys."last_name,";
                $last_name=$_POST["last_name"];
                $values=$values."'".$_POST['last_name']."',";
            }
            ...
            ...

            Check if files uploaded 
            if(isset($_FILES["zayavlenie_o_zachisleniy"]["name"])  && !empty($_FILES["zayavlenie_o_zachisleniy"]["name"])){
                array_push($uploads,"zayavlenie_o_zachisleniy");
                upload("zayavlenie_o_zachisleniy");
            }
            ...
            ...
            Here checks file for error and upload
            function upload($filename){
                    if (!file_exists('student_docs/'.$first_name.'_'.$last_name.'_'.$otchestvo)) {
                        mkdir('student_docs/'.$first_name.'_'.$last_name.'_'.$otchestvo, 0777, true);
                    }
                    $target_dir = 'student_docs/'.$first_name.'_'.$last_name.'_'.$otchestvo.'/';
                    $target_file = $target_dir . basename($_FILES[$filename]["name"]);

                    $uploadOk = 1;
                    $imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));
                    // Check if image file is a actual image or fake image
                    if(isset($_POST["submit"])) {
                       .....
                    }
                    // Check if file already exists
                    if (file_exists($target_file)) {
                        .....
                    }
                    // Allow certain file formats
                    if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
                        .....
                    }
                    // Check if $uploadOk is set to 0 by an error
                    if ($uploadOk == 0) {
                        .....
                    } else {
                        if (move_uploaded_file($_FILES[$filename]["tmp_name"], $target_file)) {

                            array_push($values,$target_file);
                            echo "The file ". basename( $_FILES[$filename]["name"]). " has been uploaded.";
                        } else {
                            echo "Sorry, there was an error uploading your file.";
                        }
                    }

                }

                $sql="UPDATE   students set $set where id='$student_id'";

                for($i=0;$i<sizeof($uploads);$i++){
                    if($i==(sizeof($uploads)-1)){
                        $sql=$sql."'".$uploads[$i]."'";
                    }
                    else{
                        $sql=$sql."'".$uploads[$i]."',";
                    }
                }
                $db->insert($sql.")");

【问题讨论】:

  • 文件上传可以试试&lt;input id="file" type="file" name="file" size="30" accept="image/jpg,image/png,image/jpeg,image/gif" /&gt;,文本输入可以试试jquery,检查this,还有here

标签: php


【解决方案1】:

您可以使用 javascript 或 php 来执行此操作。如果您正在检查该文件是否已经存在于您的网络服务器上,则不能仅使用 javascript 来执行此操作。所以你需要用 php 检查这个,因为 php 正在你的服务器上运行。 Javascript 不是。

因此,当您在上传文件时遇到任何错误(例如已经存在的文件或错误的文件格式),您可以将用户重定向到您想要的任何页面并取消请求,如下所示:

header("Location: http://YourErrorPage.com");
exit;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-08-23
    • 1970-01-01
    • 2015-05-10
    • 1970-01-01
    • 1970-01-01
    • 2012-01-06
    • 1970-01-01
    相关资源
    最近更新 更多