0

I have two drop downs, second drop down is dependable on first dropdown. for example News -> News Uploading Task

In some tasks I need an image to be uploaded and in some cases their no need of image. Where Image is not needed I just hide the File upload filed using java script but when posting we are getting error. If image is not required than after submitting I need to bypass image field checks.

I have used

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

but no success.

My code is as below

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 Code

<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>

May be question is duplicated but I have tried all options please help to resolve the issue.

I have tried mentioned solutions but no success may be I am applying not on right place so please guide me.

Thanks

neohacksus
  • 39
  • 7

0 Answers0