I’m a using the following code to output the contents of an array of lines to a file. The code works perfectly locally. The code appears to be successful when running on the server according to the debug messages. The problem is the file is unchanged. Here’s the code:
$file = 'urls.txt';
if (file_exists($file) && is_writable($file)) {
if (false !== ($handle = fopen($file, 'w'))) {
debug_log("File Open.");
fwrite($handle, implode("n", $lines));
debug_log("File Write.");
fclose($handle);
} else {
http_response_code(500);
debug_log("Failed to open file for writing.");
exit;
}
} else {
http_response_code(500);
debug_log("Failed Not Good.");
exit;
}
debug_log("File updated successfully.");
echo "Success";
Here’s an example of the debug output.
Data received: 65~/words6/scores.txt|10/23/2023 02:23
Parsed index: 65
Parsed file path: /words6/scores.txt
Parsed date: 10/23/2023 02:23
Updated line: /words6/scores.txt|10/24/2023 01:35
File Open.
File Write.
File updated successfully.
Success
The file is there because I read it just before writing it. The permissions seem to be fine (644 on the file 755 on the folder). A test script in the same folder can create and write to a file. A test script that opens ‘urls.txt’, appends to it and closes it works fine on the server. It’s just this block of code (or some other problem I’m not imagining) that’s failing me. Oh, I also tried using file_put_contents with the same result. Any clues?
Jerry Aldrich is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
1