I am trying to add a file upload ability to an internal company ticketing system. The code works fine for the actual upload. The problem I am running into is that if there is no file, the code still executes. I have tried many things, from using the $_FILES error coding to adding a select menu to choose if you want to add a file or not, but the code still runs.
It works like this:
- Ticket info is processed and entered into DB = dates, ticket info, email strings, etc
- File should then be validated and uploaded, and moved to a new folder (named the ticket number)
- File name and directory path are added to the DB.
- All info is passed to the notification email which is then sent out.
When this script runs, if there is no file, the DB entry is made, and the info is added to the email, but it links to non-existant directory.
Interesting tidbit: The code to make the new directory does NOT execute without the file, this is the correct action. Confusing thing is it's in the same block of code.
I have spent a day and half trying to figure this out without resolution. Any help is greatly appreciated.
//fileUpload
$fixedFileName = "";
$fileProcessed = 0;
if($_POST['yesFile'] == "yes") { //select option in form -
$allowedExts = array("gif", "jpeg", "jpg", "png", "txt", "pdf", "zip", "doc", "docx", "xls", "xlsx", "pcf");
$varForExt = explode(".", $_FILES["file"]["name"]);
$extension = end($varForExt);
if($_FILES["file"]["size"] < 20000000 && in_array($extension, $allowedExts)) {
if($_FILES["file"]["error"] > 0) {
//echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
$fixedFileName = print_r($_FILES);
} else {
$fixedFileName = trim(str_replace(' ','_', $_FILES["file"]["name"]));
$dirToMake = '/var/www/html/eis/uploads/ESIG/';
$dirToMake .= $_SESSION['lastRID'].'/';
mkdir($dirToMake);
if(move_uploaded_file($_FILES["file"]["tmp_name"], $dirToMake.$fixedFileName)) {
//insert filepath/file into sql
$fileProcessed = 1;
}
}
}
}