I have an upload script that works fine but now I need to make the upload non mandatory i.e user does not have to upload a file if they choose not to.
I think the problem is with the first line where I check if an upload file exists. It always seems to be true even if no file was selected for upload.
Any help or suggestions would be great.
Thanks
Here is my upload script:
//check for an uploaded file
if(isset($_FILES['upload']))
{
//validate type
$allowed = array('application/msword', 'application/pdf');
if (in_array($_FILES['upload']['type'], $allowed))
{
//upload directory
$dir = 'occurrenceuploads/';
// create random number
$random_digit = rand(0000,9999);
//create date and time
$rename_date = date("M_j_G-i-s_Y_");
//remove spaces in file name
$file= str_replace(' ','_',strtolower($_FILES["upload"]["name"]));
//add date and time to file name
$file = $file.$rename_date.$file;
//move the file over
move_uploaded_file($_FILES['upload']['tmp_name'],$dir.$file);
}
else
{
$errors[] = 'Please upload a word document or a PDF';
}
//check for error
if ($_FILES['upload']['error'] > 0)
{
//print message based on error type
switch($_FILES['upload']['error']){
case 1:
$errors[] = 'The file could not be uploaded because: The file exceeds the maximum file size (PHP)';
break;
case 2:
$errors[] = 'The file could not be uploaded because: The file exceeds the maximum file size (HTML)';
break;
case 3:
$errors[] = 'The file could not be uploaded because: The file was only partially uploaded';
break;
case 4:
$errors[] = 'The file could not be uploaded because: No file was selected';
break;
case 5:
$errors[] = 'The file could not be uploaded because: No temporary folder was available';
break;
case 6:
$errors[] = 'The file could not be uploaded because: Unable to write to disk';
break;
case 7:
$errors[] = 'The file could not be uploaded because: File upload stopped';
break;
default:
$errors[] = 'The file could not be uploaded because: An error occurred while uploading';
break;
}
}
//Delete the file if still exists
if(file_exists($_FILES['upload']['tmp_name']) && is_file($_FILES['upload']['tmp_name'])){
unlink($_FILES['upload']['tmp_name']);
}
}