PHP向文字檔累加資料

來源:互聯網
上載者:User
PHP向文字檔追加資料
代碼如下:

//一分鐘內 php檔案 被訪問5000次,每次都往文字檔裡追加 $word 資料 $word= "ok!";

//請教各位高手,如果不鎖定,追加的資料會出錯嗎,畢竟不是讀,只是一個勁的追加資料

$word= "ok!";

$fn = 'a2.txt';
$fp = fopen($fn, 'a');

flock($fp, LOCK_EX); //鎖定檔案,避免讀寫

fwrite($fp, $word);

flock($fp, LOCK_UN); //解鎖

fclose($fp); //關閉程式流

如果不影響,那麼我就不想鎖定了. 期待.....




------解決方案--------------------
讀的時候加鎖(lock_sh),寫的時候也進行加鎖(lock_ex),這樣程式才不會出現問題
------解決方案--------------------
file_put_contents( $fn, $word, LOCK_EX );
------解決方案--------------------
On versions of PHP before 5.3.2, the lock is released also by fclose() (which is also called automatically when script finished).


fclose()已經包含鎖了.
多餘的操作!
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.