duanlao6573 2019-02-21 05:24
浏览 26
已采纳

如何在通过post发送之前检查错误文件输入

In the web page I have 2 tabs and 1 submit button. I first tab inputs with type=text. In second inputs with type=file. I wanna to send both tabs with 1 submit. But problem isn't here. In the second tab I need to check files for error before sending them. So If it has an error then submit button isn't active. How can I do this? Now I check them for error only after sending them. And when some of the files have an error then upload fails and its return to page with an error. And all other files which haven't error disappear from the input . First tab http://prntscr.com/mnzc79 . Second tab http://prntscr.com/mnzca3 . Here all inputs are the document of the student in university. So I can use just multi upload. Because all of them are a different files. I can't understand the mechanism of uploading many file inputs with error check. Here my code

            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.")");
  • 写回答

1条回答 默认 最新

  • duankong8998 2019-02-21 07:50
    关注

    You can either do this with javascript or php. If you are checking if the file does already exist on your webserver, you can't do this ONLY using javascript. So you need to check this with php, cause php is running on your server. Javascript not.

    So, when you encounter any error while uploading the files (like already existing files or wrong file formats) you can redirect the user to any page you want and cancel the request like this:

    header("Location: http://YourErrorPage.com");
    exit;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 在获取boss直聘的聊天的时候只能获取到前40条聊天数据
  • ¥20 关于URL获取的参数,无法执行二选一查询
  • ¥15 液位控制,当液位超过高限时常开触点59闭合,直到液位低于低限时,断开
  • ¥15 marlin编译错误,如何解决?
  • ¥15 有偿四位数,节约算法和扫描算法
  • ¥15 VUE项目怎么运行,系统打不开
  • ¥50 pointpillars等目标检测算法怎么融合注意力机制
  • ¥20 Vs code Mac系统 PHP Debug调试环境配置
  • ¥60 大一项目课,微信小程序
  • ¥15 求视频摘要youtube和ovp数据集