I'm trying to add extra functionality to my my current form, in the way of adding an option to upload a file if a certain checkbox is checked. Most of my code is hodge podged together from other examples online.
In my form, I'm using this input. The idea is if the checkbox is checked, show a file upload field. If the checkbox is check and the column for the field is populated in the db, then show a link to it.
<input type="checkbox" name="emp_acd" id="emp_acd" value="Y" <?php if (!(strcmp($row_employees['emp_acd'],"Y"))) {echo "checked=\"checked\"";} ?>>
<?php if(!empty($row_employees['emp_acd_file'] && $row_employees['emp_acd'] == 'Y')){
echo '<a href="' . $row_employees['emp_acd_file'] . '">View Current ACD</a>';
} else if(empty($row_employees['emp_acd_file'] && $row_employees['emp_acd'] == 'Y'){
echo '<input type="file" name="emp_acd_file" class="file_upload" id="emp_acd_file">';
} ?>
Simple enough, and seems to work if I manually manipulate these fields in the database. To update the database, I'm trying to set conditionals as well. To start, I'm trying to set a null variable (is that necessary?) for my file. The checkbox is set to ) unless checked, in which case it updates the db value to Y
. Since I'm trying to only do a file update if the checkbox is checked, I'm using isset()
. From there, I'm setting my target directory for the file upload to be 3 levels up from my current working directory (this is a Windows Server, hence the naming convention. NTFS permissions have been set).
if(isset($_POST['update'])) {
$emp_acd = isset($_POST['emp_acd']) ? $_POST['emp_acd'] : 0;
$emp_acd_file = "";
if(isset($_POST['emp_acd']) ? $_POST['emp_acd'] : 0){
// Set placement folder
$target_dir = dirname(__DIR__,3) . '\Departments\HR\documents\Advance Care Directives\\';
// Get file paths
$emp_acd_file = $target_dir . basename($_FILES['emp_acd_file']['name']);
// Get file extension
$emp_acd_fileExt = strtolower(pathinfo($emp_acd_file, PATHINFO_EXTENSION));
// Allowed file types
$allowd_file_ext = array("pdf","doc","docx");
if (!file_exists($_FILES["emp_acd_file"]["tmp_name"])) {
$resMessage = array(
"status" => "alert-danger",
"message" => "Select documents to upload."
);
} else if (!in_array($emp_acd_fileExt , $allowd_file_ext)) {
$resMessage = array(
"status" => "alert-danger",
"message" => "Allowed file formats .pdf, .doc, and .docx."
);
} else if (file_exists($emp_acd_file)) {
$resMessage = array(
"status" => "alert-danger",
"message" => "File already exists."
);
} else {
move_uploaded_file($_FILES["emp_acd_file"]["tmp_name"], $emp_acd_file);
}
}
$edit = mysqli_query($conn,"UPDATE employees SET emp_acd='$emp_acd', emp_acd_file='$emp_acd_file' WHERE emp_id='$emp_id'");
}
When I try updating, I'm met with a slew of issue. For instance, if the checkbox isn't set and I try to set it and then update, I get the following errors:
- Undefined array key "emp_acd_file" (related to this line
$emp_acd_file = $target_dir . basename($_FILES['emp_acd_file']['name']);
) - Trying to access array offset on value of type null (related to this line
$emp_acd_file = $target_dir . basename($_FILES['emp_acd_file']['name']);
) - Undefined array key "emp_acd_file" (related to this line
if (!file_exists($_FILES["emp_acd_file"]["tmp_name"])) {
) - Trying to access array offset on value of type null (related to this line
if (!file_exists($_FILES["emp_acd_file"]["tmp_name"])) {
)
If I manually set the checkbox value in the db to Y
and try uploading the file, I get the same errors.