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 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么