如何检查文件字段是否为空?

时间:2017-10-03 09:19:37

标签: php upload

我有两次下降,第二次下降是可靠的第一次下拉。例如新闻 - >新闻上传任务

在某些任务中,我需要上传图像,在某些情况下,他们不需要图像。不需要图像的地方我只是使用java脚本隐藏文件上传文件但是在发布时我们收到错误。如果不需要图像而不是提交后我需要绕过图像区域检查。

我用过

if ($_FILES['cover_image']['size'] == 0 && $_FILES['cover_image']['error'] == 0)
{
    // cover_image is empty (and not an error)
}

但没有成功。

我的代码如下

if(isset($_POST["submit"])) {

    $check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
    if($check !== false) {
        echo "File is an image - " . $check["mime"] . ".";
        $uploadOk = 1;
    } else {
        echo "File is not an image.";
        $uploadOk = 0;
    }
}
// Check if file already exists
if (file_exists($target_file)) {
    echo "Sorry, file already exists.";
    $uploadOk = 0;
}
// Check file size
if ($_FILES["fileToUpload"]["size"] > 900000) {
    echo "Sorry, your file is too large.";
    $uploadOk = 0;
}


// Allow certain file formats
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg" && $imageFileType != "gif" ) {
    echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
    $uploadOk = 0;
}


// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
    echo "Sorry, your file was not uploaded.";

// if everything is ok, try to upload file
} else {
 //Writes the information to the database
} else {
echo "Sorry, there was an error uploading your file.";
}

HTML代码

<form name="testform" action="upload.php" method="post" enctype="multipart/form-data">
                                        <div class="form-group">
                                            <label>Please select Project</label>

<?php

echo "<br>Select Category first  <select name=project id='s1' class=form-control onchange=AjaxFunction();>
<option value=''>Select One</option>";

$sql="select * from projects "; // Query to collect data from table 

foreach ($pdo->query($sql) as $row) {
echo "<option value=$row[project_id]>$row[project_name]</option>";
}
?>



                </select>

                                        </div> 

                                                                                <div class="form-group">
                                            <label>Please select Task</label>
<select name=task id='s2' class=form-control></select>


                                        </div> 


                                        <div class="form-group">
                                            <label>Paste URL</label>
                                            <input class="form-control" id="url" name="url" value="">

                                            <p class="help-block">Please paste complete URL of News/Post/Video</p>
                                        </div>

                                        <script type="text/javascript">
                                            $('#s1').on('change',function(){
                                                if( $(this).val()==="7" || $(this).val()==="8"){
                                                $("div#fileUpload").hide()
                                                }
                                                else{
                                                $("div#fileUpload").show()
                                                }
                                            });                                    
                                            $('#s2').on('change',function(){
                                                if( $(this).val()==="4" || $(this).val()==="13" || $(this).val()==="20" || $(this).val()==="22" || $(this).val()==="10"  || $(this).val()==="14"  ){
                                                $("div#fileUpload").hide()
                                                }
                                                else{
                                                $("div#fileUpload").show()
                                                }
                                            });
                                        </script>

                                        <div class="form-group" id="fileUpload">
                                            <label>Upload Image File</label>
                                            <input type="file" name="fileToUpload" id="fileToUpload">
                                        </div>

                                        <button type="submit" class="btn btn-default">Save Work</button>
                                        <button type="reset" class="btn btn-default">Reset Work</button>
                                    </form>

可能是问题重复,但我已经尝试了所有选项,请帮助解决问题。

我尝试过提到的解决方案,但没有成功,我申请的不是正确的地方,所以请指导我。

由于

0 个答案:

没有答案