Home » Php » PHP check if file is in use

PHP check if file is in use

Posted by: admin July 12, 2020 Leave a comment

Questions:

I want to make a flat files database wich will use .php files to store data from the website. My only problem is that if when I ‘select’ something from the flatfile database (that means the file is readed), if in that moment a ‘update’ is in progress (that means the file is modified and written), the file gets blank and I loose all the content.

I thaught about something, to check if the file is in use at the moment, and if it is, to wait a couple of milliseconds and check again.

How to&Answers:

look into flock() http://php.net/manual/en/function.flock.php

Answer:

Please Try this It worked on my case….

    if(readyToRead(__FILE__)){
        echo "File is ready to read.";
    } else{
        echo "File is used by somebody else.";
    }

   function readyToRead($file){
       return ((time() - filemtime($file)) > 5 ) ? true : false;
   }