| 域名空间 下载中心 社区论坛 信息公告 my小屋 |
![]() |
联系我们 设为首页 加入收藏 |
|
首页 | 新闻资讯 | 编程开发 | 网页设计 | 图形图象 | 网络媒体 | 网站模板 | 数 据 库 | 投稿 论坛 | 操作系统 | 系统优化 | 网络安全 | 黑客技术 | 硬件学堂 | 硬件报价 | 服 务 器 | 地图 专题 | 应用软件 | 聊天通讯 | q q 专栏 | 建站经验 | 在线工具 | 站长club | 注 册 表 | 旧版 社会 | 游戏娱乐 | 设计欣赏 | 疑难解答 | 社区论坛 | 韩国素材 | 素材图库 | 广告服务 | 服务 |
| 新版上线![旧版] | |||||
注:打开慢时请稍等
请问php中如何防止同时对一个文件进行读写操作http://www.iyit.net 日期:2006-10-6 15:40:14 来源:iyit.net收集 点击: |
用perl写cgi时有函数可以控制防止对同一个文件进行操作,php中如何做, 谁能说一下,谢了! --------------------------------------------------------------- 试试用flock();不过这个函数不是所有系统都能用。 flock (php 3>= 3.0.7, php 4 >= 4.0.0) flock -- portable advisory file locking description bool flock ( int fp, int operation [, int wouldblock]) php supports a portable way of locking complete files in an advisory way (which means all accessing programs have to use the same way of locking or it will not work). flock() operates on fp which must be an open file pointer. operation is one of the following values: to acquire a shared lock (reader), set operation to lock_sh (set to 1 prior to php 4.0.1). to acquire an exclusive lock (writer), set operation to lock_ex (set to 2 prior to php 4.0.1). to release a lock (shared or exclusive), set operation to lock_un (set to 3 prior to php 4.0.1). if you don't want flock() to block while locking, add lock_nb (4 prior to php 4.0.1) to operation. flock() allows you to perform a simple reader/writer model which can be used on virtually every platform (including most unix derivatives and even windows). the optional third argument is set to true if the lock would block (ewouldblock errno condition) flock() returns true on success and false on error (e.g. when a lock could not be acquired). note: because flock() requires a file pointer, you may have to use a special lock file to protect access to a file that you intend to truncate by opening it in write mode (with a "w" or "w+" argument to fopen()). warning flock() will not work on nfs and many other networked file systems. check your operating system documentation for more details. on some operating systems flock() is implemented at the process level. when using a multithreaded server api like isapi you may not be able to rely on flock() to protect files against other php scripts running in parallel threads of the same server instance! --------------------------------------------------------------- 读文件: $fp=@fopen($file_name,"r"); flock($fp,lock_sh); $file_data=fread($fp,filesize($file_name)); fclose($fp); 写文件: $fp=@fopen($file_name,"w"); flock($filenum,lock_ex); $file_data=fwrite($p,$data); fclose($fp); 编辑:黑鹰 [发送给好友] [打印本页] [关闭窗口] [返回顶部] 上一篇:寻找一个有关字符串的函数 下一篇:我的数据库不能写入,为什么??? 转载请注明来源:www.iyit.net 特别声明: 本站除部分特别声明禁止转载的专稿外的其他文章可以自由转载,但请务必注明出处和原始作者。文章版权归文章原始作者所有。对于被本站转载文章的个人和网站,我们表示深深的谢意。如果本站转载的文章有版权问题请联系编辑人员,我们尽快予以更正。 |
| 最新更新 | 热点排行 | 推荐新闻 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 设置首 页 - 版权声明 - 广告服务 - 关于我们 - 联系我们 - 友情连接 |
| |||||||