Maison  >  Questions et réponses  >  le corps du texte

php有没有判断文件是否打开的函数

如题目,若是有时需要在文件中增加内容,怎么保证写的东西不会被另一个人也打开在里面写?

我想的是写操作开始之前随便生成一个东西,操作完成之后,再把这东西给删除啦;

淡淡烟草味淡淡烟草味2701 Il y a quelques jours595

répondre à tous(1)je répondrai

  • PHP中文网

    PHP中文网2017-05-16 13:00:28

    Verrouillage de fichiers

    <?php
    
    $fp = fopen("/tmp/lock.txt", "r+");
    
    if (flock($fp, LOCK_EX)) {  // 进行排它型锁定
        ftruncate($fp, 0);      // truncate file
        fwrite($fp, "Write something here\n");
        fflush($fp);            // flush output before releasing the lock
        flock($fp, LOCK_UN);    // 释放锁定
    } else {
        echo "Couldn't get the lock!";
    }
    
    fclose($fp);
    
    ?>

    De : http://php.net/manual/zh/func...

    répondre
    0
  • Annulerrépondre