doujiong2533 2014-12-16 16:38
浏览 14
已采纳

联系表单不允许Null在文件上传PHP

I want my contact form will to allow Nulls on file upload. when I try and submit the form without a file uploaded it says "Sorry, file already exists., Sorry, only JPG, JPEG, PNG, GIF, TIF, EPS and PSD files are allowed ,Sorry, your file was not uploaded." How would I change the code to bypass this when it is emtpy or isnt this safe ? if I haven't added enough detail please let me know and i will previde it.

PHP

  $target_dir = "uploads/";
            $target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
            $uploadOk = 1;
            $imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
            // Check if image file is a actual image or fake image

            // Check if file already exists
            if (file_exists($target_file)) {
                echo '<p style="color:red;">Sorry, file already exists.</p>';
                $uploadOk = 0;
            }
            // Check file size
            if ($_FILES["fileToUpload"]["size"] > 150000000) { // Byte = 150MB
                echo '<p style="color:red;">Sorry, your file is larger than 150MB.</p>';
                $uploadOk = 0;
            }
            // Allow certain file formats
            if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
            && $imageFileType != "gif" && $imageFileType != "eps" && $imageFileType != "tiff" 
            && $imageFileType != "psd") {
            echo '<p style="color:red;">Sorry, only JPG, JPEG, PNG, GIF, TIF, EPS and PSD files are allowed.</p>';
                $uploadOk = 0;
            }
            // Check if $uploadOk is set to 0 by an error
            if ($uploadOk == 0) {
                die('<p style="color:red;">Sorry, your file was not uploaded.</p>');
            // if everything is ok, try to upload file
            } else {
                if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
                    echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
                } else {
                    echo '<p style="color:red;">Sorry, there was an error uploading your file.</p>';

                }
            }
  • 写回答

1条回答 默认 最新

  • duanqiao3608 2014-12-16 17:13
    关注

    You can check it:

    iF($_FILES["fileToUpload"] == UPLOAD_ERR_NO_FILE) {
        // no file selected, do skip
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥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的速度时间图像)我想问线路信息是什么