简体   繁体   中英

File upload optional on PHP form submit

I have a form which has an option to upload a file too, however, currently, the form throws error 4 if there is no file uploaded. I want to make the file upload optional and not mandatory. What do I have to change on the code below to achieve that? Any help would be appreciated.

<?php
include('db.php');
if (!isset($_FILES['documents']['tmp_name'])) {
    echo "";
}else{
    $file=$_FILES['documents']['tmp_name'];
    $image = $_FILES["documents"] ["name"];
    $image_name= addslashes($_FILES['documents']['name']);
    $size = $_FILES["documents"] ["size"];
    $error = $_FILES["documents"] ["error"];
    if ($error > 0){
        die("Error uploading file! Code $error.");
    }else{
        if($size > 10000000) //conditions for the file
        {
            die("Format is not allowed or file size is too big!");
        } else {
            move_uploaded_file($_FILES["documents"]["tmp_name"],"upload/" . $_FILES["documents"]["name"]);          
            $documents=$_FILES["documents"]["name"];
            $emp_id= $_POST['emp_id'];
            $name= $_POST['name'];
            $title= $_POST['title'];
            $department= $_POST['department'];
            $leavetype= $_POST['leavetype'];
            $startdate= $_POST['startdate'];
            $enddate= $_POST['enddate'];
            $comments= $_POST['comments'];
            $status= $_POST['status'];
            $user= $_POST['user'];
            mysql_query("insert into medical (emp_id, name, title, department, leavetype, startdate, enddate, comments, documents, status, user) 
                         values('$emp_id', '$name','$title', '$department', '$leavetype', '$startdate', '$enddate', '$comments', '$documents', '$status', '$user')") or die(mysql_error());
        }
        header('Location:index.php');
    }
}
?>

Here is the form:

 <form method="post" action="adds.php"  enctype="multipart/form-data">
                    <table class="table1">
                        <tr>
                            <td><label style="color:#3a87ad; font-size:18px;">Employee ID</label></td>
                            <td width="30"></td>
                            <td><input type="text" name="emp_id" placeholder="Employee Number" required /></td>
                        </tr>
                        <tr>
                            <td><label style="color:#3a87ad; font-size:18px;">Full name</label></td>
                            <td width="30"></td>
                            <td><input type="text" name="name" placeholder="Full Name" required /></td>
                        </tr>
                        <tr>
                            <td><label style="color:#3a87ad; font-size:18px;">Job Title</label></td>
                            <td width="30"></td>
                            <td><input type="text" name="title" placeholder="Job Title"  /></td>
                        </tr>
                        <tr>
                            <td><label style="color:#3a87ad; font-size:18px;">Department</label></td>
                            <td width="30"></td>
                            <td><input type="text" name="department" placeholder="Department"  /></td>
                        </tr><tr>
                            <td><label style="color:#3a87ad; font-size:18px;">Type of Leave:</label></td>
                            <td width="30"></td>
                            <td><select name="leavetype" >
                                    <option value="Medical Leave">Medical Leave</option>
                                    <option value="Personal Leave">Personal Leave</option>
                                    <option value="Recruitment fairs/Interviews">Recruitment fairs/Interviews</option>
                                    <option value="Paternity Leave">Paternity Leave</option>
                                    <option value="Maternity Leave">Maternity Leave</option>
                                    <option value="Compassionate Leave">Compassionate Leave</option>
                                    <option value="Marriage Leave">Marriage Leave</option>
                                    <option value="Bereavement Leave">Bereavement Leave</option>
                                    <option value="Professional Development">Professional Development</option>
                                </select></td>
                        </tr><tr>
                            <td><label style="color:#3a87ad; font-size:18px;">Start Date:</label></td>
                            <td width="30"></td>
                            <td><input type="date" id="startdate" name="startdate" placeholder="0000-00-00"/></td>
                        </tr><tr>
                            <td><label style="color:#3a87ad; font-size:18px;">End Date:</label></td>
                            <td width="30"></td>
                            <td><input type="date" id="enddate" name="enddate" placeholder="0000-00-00"/></td>
                        </tr><tr>
                            <td><label style="color:#3a87ad; font-size:18px;">Additional Notes:</label></td>
                            <td width="30"></td>
                            <td><input type="text" id="comments" name="comments"/></td>
                        </tr>
                        <tr>
                            <td><label style="color:#3a87ad; font-size:18px;">Supporting Documents</label></td>
                            <td width="30"></td>
                            <td><input type="file" name="documents" /></td>
                        </tr>
                        <tr>
                            <td><label style="color:#3a87ad; font-size:18px;">Status</label></td>
                            <td width="30"></td>
                            <td><input type="text" id="status" name="status" value="Pending" readonly></td>
                        </tr>
                        </tr><tr>
                            <td><label style="color:#3a87ad; font-size:18px;">Signature:</label></td>
                            <td width="30"></td>
                            <td><input type="text" id="user" name="user" value="<?php echo strtoupper(substr($_SERVER["AUTH_USER"], 4));?>" readonly></td>
                        </tr>


                    </table>


    </div>
    <div class="modal-footer">
    <button class="btn" data-dismiss="modal" aria-hidden="true">Close</button>
<button type="submit" name="Submit" class="btn btn-primary">Add</button></div></form>

use this minimum line of code

<?php
    include('db.php');
    $emp_id= $_POST['emp_id'];
    $name= $_POST['name'];
    $title= $_POST['title'];
    $department= $_POST['department'];
    $leavetype= $_POST['leavetype'];
    $startdate= $_POST['startdate'];
    $enddate= $_POST['enddate'];
    $comments= $_POST['comments'];
    $status= $_POST['status'];
    $user= $_POST['user'];
    $document = "";
//if file is empty directly insert in table 
    if (isset($_FILES['documents']) && is_array($_FILES['documents']) && count($_FILES['documents']) > 0) {
        $file=$_FILES['documents']['tmp_name'];
        $image = $_FILES["documents"] ["name"];
        $image_name= addslashes($_FILES['documents']['name']);
        $size = $_FILES["documents"] ["size"];
        $error = $_FILES["documents"] ["error"];
        if ($error > 0){
            die("Error uploading file! Code $error.");
            exit;
        }else{
            if($size > 10000000) //conditions for the file
            {
                die("Format is not allowed or file size is too big!");
                exit;
            }else{
                move_uploaded_file($_FILES["documents"]["tmp_name"],"upload/" . $_FILES["documents"]["name"]);          
                $documents=$_FILES["documents"]["name"];
            }
        }
    }
    mysql_query("insert into medical (emp_id, name, title, department,leavetype, startdate, enddate, comments,status, user) values('$emp_id', '$name','$title', '$department', '$leavetype', '$startdate', '$enddate', '$comments','$status', '$user')") or die(mysql_error());
    header('Location:index.php');
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM