I have a php script that executes Python scripts for the desired excel workbook uploaded from a HTML by user interface, the script work fine, but when I am using readfile() function to download the current file after its being processed, I am facing issues trying to open it, (file format of file extension is not valid)
the result excel file size seems to be correct along with the extension, how every I am still facing this issue bellow you can find my script:
Note that office 365 is installed in both server and my pc.
<?php
ini_set('max_execution_time', 400);
if(isset($_POST['upload'])) {
$targetDir = "C:\\SSProcess\\ExcelFiles\\";
if (!file_exists($targetDir)) {
mkdir($targetDir, 0777, true);
}
$excel_path = $targetDir . basename($_FILES["excelFile"]["name"]);
$fileType = strtolower(pathinfo($excel_path, PATHINFO_EXTENSION));
if ($fileType != "xls" && $fileType != "xlsx") {
echo "Only Excel files are allowed.";
exit();
}
if (move_uploaded_file($_FILES["excelFile"]["tmp_name"], $excel_path)) {
echo "The file ". basename( $_FILES["excelFile"]["name"]). " has been uploaded.";
echo "file path = ", $excel_path, " .";
$pythonScript1 = "C:/Users/bot_runner2/AppData/Local/Programs/Python/Python310/python.exe C:/Users/bot_runner2/Desktop/SS/Script/DuplicatedPerType.py " . escapeshellarg($excel_path);
exec($pythonScript1, $output1, $returnCode1);
if ($returnCode1 === 0) {
echo "DuplicatedPerType.py executed successfully.";
$pythonScript2 = "C:/Users/bot_runner2/AppData/Local/Programs/Python/Python310/python.exe \"C:/Users/bot_runner2/Desktop/SS/Script/Type&SubType.py\" " . escapeshellarg($excel_path);
exec($pythonScript2, $output2, $returnCode2);
if ($returnCode2 === 0) {
echo "Type&SubType.py executed successfully.";
$newDir = "C:\\SSProcess\\ProcessedFiles\\";
$newExcelPath = $newDir . basename($_FILES["excelFile"]["name"]);
if (!file_exists($newDir)) {
mkdir($newDir, 0777, true);
}
if (copy($excel_path, $newExcelPath)) {
echo "Excel file copied successfully.";
sleep(3);
header("Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
header("Content-Disposition: attachment; filename=\"" . basename($newExcelPath) . "\"");
readfile($newExcelPath);
exit();
} else {
echo "Failed to copy Excel file.";
}
} else {
echo "Error executing Type&SubType.py.";
}
} else {
echo "Error executing DuplicatedPerType.py.";
}
} else {
echo "Sorry, there was an error uploading your file.";
}
}
?>
What I have tried:
i have tried setting the appriopriate contect-type header for xlsx files.