У меня есть форма, в которой есть возможность загрузить файл, однако в настоящее время форма выдает ошибку 4, если файл не загружен. Я хочу сделать загрузку файла необязательной, а не обязательной.
Что мне нужно изменить в приведенном ниже коде, чтобы добиться этого? Любая помощь будет оценена.
<?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');
}
}
?>
Вот форма:
<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>