Ihaveaphpscriptthatlogsads(banners)forawebsiteandstoresthemtoa.datfile.InsidethisfileanID,URL,anotherimportantinformationissaved.TheproblemIamhavingisthatthereareatanygiventime4adsonthepageandsothe.datfileisoftencorruptedwhenthephpscriptattemptstowritetoidarkedeneurope.comhileitisopen. Icheckedandtriedthissolutionhoweveritdidnothelpme: PHPSimultaneousfileaccess/flock()issue

ThefunctionIamusingatthemomentlookslikethis:

functionwriteads(){global$bannerAdsPath,$ads,$bannerAds;$data=fopen($bannerAdsPath,”w”)ordie();flock($data,2)ordie();fputs($data,

Đang xem: Mở file trong php

join(”
“,$ads).”
“);while(list($key,$val)=each($bannerAds)){if(($key!=””)&&($val!=””)){fputs($data,$key.”=”.$val.”
“);}}flock($data,3);fclose($data);reset($bannerAds);}AnyhelpwouldbeappreciatedasIhavebeenscratchingmyheadoverthisforawhile. Sidebitofinformation,theclientdidnodarkedeneurope.comanttohavetheircoderewrittentouseaDatabaseinsteadofafilesothatoptionisout.

Thanks!

參考解法

方法 1:

fopenwith”w”truncatesthefilebeforeyouhavetheoptionofflockingit.

方法 2:

Youalmostneverwanttouseflocktounlockafile;justusefclose;thefilewillbeunlockedwhenthehandleisclosed,andthadarkedeneurope.comayyouknowthatnobufferedwriteswillhappenafteryouunlock.

(by Jingles177、Iván、hobbs)

參考文件

#flock #fopen #fclose #PHP

相關問題

PHPflock() – 引擎蓋下是什麼? (PHP flock() – what's under the hood?)
如何使用linuxflock命令來防止另一個root進程刪除文件? (How do I use the linux flock command to prevent another root process from deleting a file?)
TCL、Linux 和 FLOCK (TCL, Linux, and FLOCK)
如何使用 File#flock 對獨占鎖發出非阻塞請求? (How can I make a non-blocking request for an exclusive lock using File#flock?)
Viết Flock PHP để mở tệp (PHP Flock Writing to Open File)
安裝 raw-socket 時 node-gyp 構建錯誤 (node-gyp build error while installing raw-socket)
使用flock在bash中讀取和寫入文件 (Using flock to read and write to a file in bash)
有沒有一種可移植的方法來設置flock() 的超時時間? (Is there a portable way to put a timeout on flock()?)

留言討論

提交送出

*

編輯

Xem thêm:

提交送出

*

編輯
提交送出
回覆
提交送出
解決方案
社群
公司
Country/Region Global(English) Taiwan(繁體中文) Japan (日本語) South Korea (한국인) Singapore (English) Indonesia (Bahasa) Việt Nam (Tiếng Việt) Thailand (Thai)
歡迎回家 ×
使用 Github 帳號登入 使用 Google 帳號登入
取消
免費加入 darkedeneurope.com ×
使用 Github 帳號註冊 使用 Google 帳號註冊

Xem thêm: Trong Javascript, Làm Cách Nào Để Kiểm Tra Phần Tử Trùng Trong Mảng Javascript

取消

Related Post

Leave a Reply

Your email address will not be published. Required fields are marked *