Jump to content

unlink first, then file_put_contents() is behaving weird


JacobSeated
Go to solution Solved by ginerjm,

Recommended Posts

I have some code that calls unlink() before calling file_put_contents() with file_append; I expect it to delete the file when the script runs, create a new file for the specific request, and only append data for this current process.

BUT, the file seems to be deleted twice when unlink() is called first. Once at the start of the execution, and again when execution ends.

Suspecting that a timeout might help, I added two seconds timeout after unlink().

if (file_exists('dev-log.txt')) {
    unlink('dev-log.txt');
    sleep(2);
} 

if(false === file_put_contents('dev-log.txt', $data, FILE_APPEND)) {
   throw new Exception("Unable to save log");
}

I found that if I apply sleep(10) at the end of the script instead, it does in fact write data to the file, but then the file is ultimately deleted again when the script exits, which I find very strange.

The same basically happens if I try to truncate the file. I am on PHP 8.0 with PHP8.0-fpm + Apache.
 

Link to comment
Share on other sites

Sorry to have troubled you 🙂

I spent several hours trying to fix this issue today, and it now seems the problem just resolved itself.

I suspect it was a combination of different issues, one involving me forgetting to clear the obcache while testing. E.g.:

 

opcache_invalidate(__FILE__, true);

ini_set('display_errors', '1');

ini_set('display_startup_errors', '1');

error_reporting(E_ALL);

I also had a very large file, which I have now split into 1 file per class. I am not sure if that could have caused it. I had quite a few classes located at the bottom of my script. Now everything is also at the top.

Link to comment
Share on other sites

14 hours ago, ginerjm said:

Have you done a search of your script for another call to unlink?

Yes thank you. I am working on creating an app that uses a certain API, and did not want to post everything here. I just started coding one-big-file, intending to organize stuff properly later, and I only used unlink for the log file.

Because of this problem I ended up organizing it now instead of later, but I realize this way I also avoid spending time on scrolling up and down so much, and it is much easier to debug.

What is interesting to me is that the problem just seems to have solved itself, so I am not entirely sure what caused it. But as mentioned, maybe the problem was already fixed, and I just did not notice it because of caching issues. I have spent hours just moving code around, testing, trying a simplified version in a separate file — which did not have the problem. But, the file was deleted, written to, and deleted again — that's for sure — the sleep(10) at the end revealed to me it was happening.

I got some idea about what I can test to reproduce the problem:

  • Maybe I was being too optimistic in regards to placing functions and classes at the bottom of my script, only to use/initialize them at the top. I do not normally do that, but it just seemed like a good way to get them "out of my way" until I had better time and energy to organize my code. It was not 🙂 Typically you just spend time scrolling up and down to find things instead of coding..
  • It could be related to my use of singleton for the logger class, since I do not normally use singleton. I prefer creating everything with dependency injection. This is unlikely though, as I had also tried creating just a global function for the logger, and it had the same problem.
  • Related to opcache, and me not realizing the file was not always updating when I made changes. But, opcache should be disabled in my PHP-FPM config, so again not sure.
  • Related to declaring an object global inside a function. It appeared like the problem went away whenever I removed the global object declaration — but I needed it 😛 So, instead I just moved to DI, which I am now more familiar with using anyway.

I am not sure I will try to reproduce it at this point, since I got delayed so much by this. But thanks 🙂

Edited by JacobSeated
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.