简体   繁体   English

PHP 检查文件是否正在使用

[英]PHP check if file is in use

I want to make a flat files database which will use.php files to store data from the website.我想制作一个平面文件数据库,它将使用.php 文件来存储来自网站的数据。 My only problem is that if when I 'select' something from the flatfile database (that means the file is read), if in that moment a 'update' is in progress (that means the file is modified and written), the file gets blank and I lose all the content.我唯一的问题是,如果当我从平面文件数据库中“选择”某些内容(这意味着文件已被读取),如果在那一刻“更新”正在进行中(这意味着文件已被修改和写入),则文件获取空白,我失去了所有的内容。

I thought 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.我想到了一些事情,检查文件是否正在使用,如果是,等待几毫秒并再次检查。

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;
   }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM