I have a form and when i submit it, it will validate if the file exist or not. The Validation works if I choose a file to upload. When I don't need to upload a file, I leave the choose file empty, the validation does not work and the script always shows that the file already exist even though I don't need to upload a file and the input type="file"
is not required
Here is my Code:
<form action="../function/add_pre.php" method="post" enctype="multipart/form-data" >
<table class="table table-bordered">
<tr>
<td><label class="form-control">Attach and Upload your Proposal for Purchase Request:</label></td>
<td><input class="form-control" type="file" name="file" title="Click here to select file to upload."></td>
</tr>
</table>
<button name="submit" type="submit" class="btn btn-info"><i class="fa fa-paper-plane"></i> Submit</button>
</form>
This is the add_pre.php
if(isset($_POST['submit'])){
if (file_exists("../employee/" . $_FILES["file"]["name"])){
echo '<script language="javascript">alert(" Sorry!! Filename Already Exists...") </script>';
echo '<script language="javascript">window.history.back();</script>';
}
else{
move_uploaded_file($_FILES["file"]["tmp_name"],
"../employee/" . $_FILES["file"]["name"]) ;
$sql = "INSERT INTO purchase_request_file (pr_no,file) VALUES ('" .$pr_no."','" .
$_FILES["file"]["name"] ."');";
if (!mysql_query($sql))
echo('Error : ' . mysql_error());
else
echo"Success!";
}
I need to echo"Success!"
even though i submitted the form without a file.