免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 6724 | 回复: 1
打印 上一主题 下一主题

[参考] 文件操作改进精简版. :p 效率不高就是了 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2004-02-08 00:58 |只看该作者 |倒序浏览
刚才发文件锁定心得时有网友说用 rename(), 以下是小弟以前的测试程序,顺便贴上来,请指教.


  1. <?php
  2. /* ---------------------------------------------------- */
  3. /* f_lock.php(安全文件操作机制)                         */
  4. /* ---------------------------------------------------- */
  5. /* Target: 摒弃系统提供的 flock, 自己写 f_open          */
  6. /* Author: hightman.bbs@bbs.hightman.net                */
  7. /* Create: 2002/09/29                                   */
  8. /* Update: //                                           */
  9. /* Syntax: $fd = f_open($fpath, MODE); f_close($fd)     */
  10. /* ---------------------------------------------------- */

  11. // $Id: $

  12. require ("my_timer.php");
  13. $time = my_timer::start();

  14. define("_LOCKED_EXT_", ".LOCK");// .LOCK 作后缀
  15. define("_WAIT_TIMES_",100);// 20 是 1 秒, 最多等 5秒

  16. global $LOCKED_FD;
  17. $OPENED_FD = Array();// fd <=>; fpath
  18. $OP_CWD = getcwd();

  19. function clear_locked($errno = 0, $error = \'\') {
  20.     global $OPENED_FD, $OP_CWD;
  21.     chdir($OP_CWD);
  22.     foreach($OPENED_FD as $tmp)
  23. rename($tmp . _LOCKED_EXT_, $tmp);
  24.     $OPENED_FD = Array();
  25.     if ($errno) {
  26. echo "<p>;<b>;Error(" . $errno . "): </b>;" . $error . "</p>;\\n";
  27.     }
  28. }

  29. function f_lock($fpath) {
  30.     if (empty($fpath)) return;
  31.     $fpath2 = $fpath . _LOCKED_EXT_;

  32.     $i = 0;
  33.     while (file_exists($fpath2)) {
  34. $i++;
  35. usleep(50000);
  36. if ($i == _WAIT_TIMES_) return -1;
  37.     }

  38.     return rename($fpath, $fpath2);
  39. }

  40. function f_unlock($fd) {
  41.     global $OPENED_FD;

  42.     if (!is_resource($fd)) return;
  43.     $fpath = $OPENED_FD[$fd];
  44.     rename($fpath . _LOCKED_EXT_, $fpath);
  45.     unset($OPENED_FD[$fd]);
  46. }

  47. function f_open($fpath, $mode) {
  48.     global $OPENED_FD;

  49.     f_lock($fpath);
  50.     $fd = fopen($fpath . _LOCKED_EXT_, $mode);
  51.     $OPENED_FD[$fd] = $fpath;
  52.     return ($fd);
  53. }

  54. function f_close($fd) {
  55.     fclose($fd);
  56.     f_unlock($fd);
  57. }

  58. /////////////////////////////////////////////////////////////////////////

  59. error_reporting(E_ALL ^ E_NOTICE);
  60. register_shutdown_function("clear_locked");

  61. if (set_error_handler("clear_locked")) {
  62.     exit("<B>;ERROR: </B>; <SMALL>;Don\'t use function: set_error_handler() Before using f_open/close() plz ...</SMALL>;");   
  63. }

  64. $fd = f_open("./1.txt", "r");
  65. echo fread($fd, 128);
  66. f_close($fd);
  67. $time->;over();
  68. ?>;

复制代码



//附: my_timer.php

  1. <?php

  2. class my_timer {
  3.     var $time_start = 0;

  4.     function get_mtime(){
  5. list($usec, $sec) = explode(' ', microtime(), 2);
  6. return ((float)$usec + (float)$sec);
  7.     }

  8.     function &start() {
  9. if (!is_object($this)) {
  10.     $obj = new my_timer;
  11.     $obj->;start();
  12.     return ($obj);
  13. }
  14. $this->;time_start = $this->;get_mtime();
  15.     }

  16.     function over() {
  17. $time = $this->;get_mtime() - $this->;time_start;
  18. echo \"<p align=\"center\">;<small>;Processed time: $time secs.</small>;</p>;n\";
  19.     }
  20. }

  21. ?>;
复制代码

论坛徽章:
1
荣誉版主
日期:2011-11-23 16:44:17
2 [报告]
发表于 2004-02-08 01:05 |只看该作者

[参考] 文件操作改进精简版. :p 效率不高就是了

建议将此帖做为刚才“PHP文件锁定读写的一点注意”的跟帖,这样比较好一些
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP