A very narrow problem here. Trying to upload excel file with ext 'xlsm' The code runs but it saves the file with 'xlsx' extension instead, and then cannot open the file beacsue the extension is wrong.
Have this:
$finfo = new finfo(FILEINFO_MIME_TYPE);
if (false === $ext = array_search($finfo->file($_FILES['upfiles1']['tmp_name']),
array(
'doc' => 'application/msword',
'docx' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
'pdf' => 'application/pdf',
'xlsx' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
'xlsm' => 'application/vnd.ms-excel.sheet.macroEnabled.12', // macro xls files
'csv' => 'application/vnd.ms-excel',
'xls' => 'application/vnd.ms-excel'),
true
)) {
throw new RuntimeException('Invalid file format.');
}
$fullname = $_POST['filename'] . "." . $ext;
$fulldir = sprintf($_SERVER['DOCUMENT_ROOT'] . '\xxx\%s', $fullname);
if (file_exists($fulldir)) {
$okproc = $okproc + 1;
if (!unlink($fulldir)) {
$status = "old file" . $fullname . " cannot be replaced due to an error. Contact admin.";
}
} else {
$sqlFileLog = "INSERT INTO FilesLogExt (FileGroup, RefID, DateUploaded, FileName) VALUES ('2', '" . $tID . "', SYSDATETIMEOFFSET(), '" . $fullname . "'); ";
$result = $conn->query($sqlFileLog);
if($result) {
$okproc = $okproc + 1;
}
}
if (!move_uploaded_file($_FILES['upfiles1']['tmp_name'], $fulldir)) {
throw new RuntimeException('Failed to move uploaded file.');
} else {
$okproc = $okproc + 1;
}
I tried to change the order but it still save macro file into ext 'xlsx'
Appreciate any advice! Thank you!