I have two different PHP pages, both with backends and front ends, driven by Vue, thus there is an accompanying .js file for each page.
In one page, lets say WebPageA I successfully upload a file, use fetch in the .js file to send it via POST to my backend, gather some information from the upload, unpack some data and send it back to the .js script via the fetch response. Later down the line in WebPageA, after some user input, I then send another POST via fetch to my backend and in the case of WebPageA I have access to the temporary file still. It did not get deleted via PHP's temporary file structure of deleting tmp files if they are not moved or renamed. As you'll see in my code I post below, I do not rename or move the temp file and yet I have access to it when I POST to my backend the second time. Here is that code:
include "../../classes/psql_connector.php";
if (empty($_POST['req'])) {
exit();
}
if ($_POST['req'] === 'file_info' || $_POST['req'] === 'generate_plot') {
if (empty($_FILES)) {
exit();
}
$df = $_FILES['datafile'] ?? false;
if (!$df) {
echo json_encode(['success' => false]);
exit();
}
$ext = pathinfo($df['name'], PATHINFO_EXTENSION);
if (!in_array(strtolower($ext), ['tdms', 'lvm', 'csv'])) {
echo json_encode([
'success' => false,
'status' => 'file_extension'
]);
exit();
}
}
if ($_POST['req'] === 'file_info') {
$cmd = "python C:\xampp\htdocs\luwak\pages\gp\unpacker.py " . $df['tmp_name'] . " " . $ext;
$output = shell_exec($cmd. ' 2>&1 &');
$info = json_decode($output);
echo json_encode([
'success' => $info !== null,
'status' => 'done',
'info' => $info,
'output' => $output,
]);
This is the first portion of my backend for WebPageA. When I post here again from my .js file I use the request code (req) 'generate_plot' where I have access to $df['tmp_name']
and $ext
even though I didn't save or rename the uploaded file the last time this PHP script ran.
Now on my second webpage, WebPageB, I do the exact same thing. The first 25 lines of the backend PHP file for these two webpages are nearly identical, the only difference being I don't check for .lvm or .csv file types on $ext
. When I first POST to WebPageB's backend, it works as intended the file is processed and data is unpacked. However after the user inputs some information and I POST a new FormObject to my backend with a different req code, $_FILES
is now empty, and of course $df
and $ext
are not available as those variables cannot be initialized without the necessary temp file. Here is the backend code for WebPageB:
<?php
include "../../classes/psql_connector.php";
if (empty($_POST['req'])) {
exit();
}
if ($_POST['req'] === 'file_info') {
if (empty($_FILES)) {
exit();
}
$df = $_FILES['datafile'] ?? false;
if (!$df) {
echo json_encode(['success' => false]);
exit();
}
$ext = pathinfo($df['name'], PATHINFO_EXTENSION);
if ($ext != 'tdms') {
echo json_encode([
'success' => false,
'status' => 'file_extension'
]);
exit();
}
}
if ($_POST['req'] === 'file_info') {
$cmd = "python C:\xampp\htdocs\luwak\pages\cc\unpacker.py " . $df['tmp_name'] . " " . $ext;
$output = shell_exec($cmd. ' 2>&1 &');
$info = json_decode($output);
echo json_encode([
'success' => $info !== null,
'status' => 'done',
'info' => $info,
'output' => $output,
]);
I cannot for the life of me figure out why in one situation with nearly identical code a temp file is not being deleted while in the other it is.
Just to be clear I am not sending a POST via a form in the HTML produced by the frontend PHP script. I am using the fetch method within my Vue js file, here is an example:
fetch("pages/gp/gpBackend.php", {
method: "POST",
body: v.form_data_obj
})
.then((response) => {
return response.json()
})
.then(function (data) {
v.processing = false
// handle errors first
if (!data.success) {
if (data.status === 'file_extension') {
v.status_msg = "This tool only supports CSV or TDMS file format."
v.has_error = true
} else {
v.error_details = data.output
v.status_msg = "An error occurred and your file could not be processed."
v.has_error = true
}
return
}
v.status_msg = "File processed successfully. Please choose what data to plot below."
v.grouped_axis_info = data.info.group_channels
v.data_file_info = data.info
v.x_end_default = data.info.death_timestamp
v.clearError()
I am more than happy to clear up any ambiguities that might show themselves in my question, but if anyone has any insight, or perhaps there's some face-palm worthy solution I am overseeing, please let me know.
Thank you
question from:
https://stackoverflow.com/questions/65926788/why-are-my-temporary-files-being-deleted-on-one-php-page-but-not-another