[英]File upload optional on PHP form submit
我有一個也可以選擇上傳文件的表單,但是,當前,如果沒有上傳的文件,該表單將引發錯誤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>
使用此最小代碼行
<?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');
?>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.