我在服务器上运行的 Docker 容器遇到问题。我有一个 PHP 脚本,当用户第一次访问该网站时,它会创建一个 CSV 文件(例如 test_visit_events_unfinished.csv)。该文件记录与站点上的实验相关的数据。用户完成实验后,JavaScript 函数会以 application/x-www-form-urlencoded 格式向我的 PHP 脚本发送 POST 请求。此时,PHP 脚本应将文件重命名为 test_visit_events_finished.csv。
创建新的 CSV 文件后,其名称格式为
test_visit_events_unfinished.csv
。如果触发操作 finish
,则文件应重命名为 test_visit_events_finished.csv
,而不修改其内容。
我在重命名之前多次尝试检查文件是否存在,但没有抛出任何错误,而是创建了一个新文件。我已经验证了该目录的写入权限,我怀疑该问题可能是由于用户缺乏对此特定文件的权限造成的。如何动态为文件分配必要的权限?
我的php逻辑,文件的路径是正确的。所以追加和初始化操作确实有效: 文件目录定义为:
define('LOGDIR', '/var/www/html/logs');
define('LOGEXT', ".csv");
// Check if 'action' key exists in POST data
if (isset($_POST['action']) && $_POST['action'] === "init") {
// Get name of log file, and ensure it's named with _unfinished by default
$fid = $_POST['task'] ?? 'default_task'; // Default task if 'task' is missing
$unfinishedFileName = $fid . "_unfinished" . LOGEXT; // Append _unfinished to the filename
// Save data for the first time
$header = "cursor,timestamp,xpos,ypos,event,xpath,attrs,extras" . PHP_EOL;
file_put_contents(LOGDIR . "/" . $unfinishedFileName, $header . $info_data);
// Notify recording script
echo round(microtime(true) * 1000);
} else if (isset($_POST['action']) && $_POST['action'] === "append") {
// Get the log file name, use _unfinished if not specified
$fid = $_POST['task'] ?? 'default_task'; // Default task if 'task' is missing
$unfinishedFileName = $fid . "_unfinished" . LOGEXT; // Append _unfinished to the filename
// Don't write blank lines
if (trim($info_data)) {
file_put_contents(LOGDIR . "/" . $unfinishedFileName, $info_data, FILE_APPEND);
}
} else if (isset($_POST['action']) && $_POST['action'] === "finish") {
// Get the log file name without the suffix
$fid = $_POST['taskname'] ?? 'default_task'; // Default task if 'taskname' is missing
$unfinishedFileName = $fid . "_unfinished" . LOGEXT; // File with _unfinished suffix
$finishedFileName = $fid . "_finished" . LOGEXT; // File with _finished suffix
$originalFilePath = LOGDIR . "/" . $unfinishedFileName;
$finishedFilePath = LOGDIR . "/" . $finishedFileName;
if (file_exists($originalFilePath)) {
// Check if the _finished file already exists to avoid overwriting
if (!file_exists($finishedFilePath)) {
// Rename the file to mark it as finished
if (!rename($originalFilePath, $finishedFilePath)) {
exit('Failed to mark file as finished');
}
echo 'File marked as finished';
} else {
exit('A file with _finished already exists');
}
} else {
exit('Original _unfinished file not found');
}
}
因此,“重命名”的文件仍然位于正确的目录中,但没有重命名旧文件,只是创建了一个新文件。
这是我的 docker 逻辑,用于授予在包含文件的目录中写入的权限:
RUN usermod -u 1000 www-data && \
groupmod -g 1000 www-data && \
chown -R www-data:www-data /var/www/html && \
chmod -R 755 /var/www/html/logs
这里是应该发送文件名以更改名称的 JavaScript 函数:
async function finishExperiment(eventFile) {
console.log("Now savint to: ", eventFile);
// Send a request to the server to mark the log file as "finished"
try {
const response = await fetch("static/php/writeEvents.php", {
method: "POST",
headers: {
"Content-Type": "application/x-www-form-urlencoded" // Set content type to 'x-www-form-urlencoded'
},
body: new URLSearchParams({
action: "finish", // The action type to perform
taskname: eventFile // The filename to mark as finished
})
});
// Handle the response
const data = await response.text(); // Assuming the server sends a plain text message
} catch (error) {
console.error("Error finishing the experiment:", error);
}
问题是我在完成操作后调用了追加操作,因此由于后缀而创建了一个新文件。