Your Ad Here
首页 | 编程语言 | 网站建设 | 游戏天堂 | 冲浪宝典 | 网络安全 | 操作系统 | 软件时空 | 硬件指南 | 病毒相关 | IT 认证
软讯网络 > 软件时空 > 软件相关 > PHP TIPS:关于状态函数应用file_exists()和clearstatcache();
【标  题】:PHP TIPS:关于状态函数应用file_exists()和clearstatcache();
【关键字】:PHP,TIPS,file_exists,clearstatcache
【来  源】:http://blog.csdn.net/abaowu/archive/2006/04/16/665195.aspx

PHP TIPS:关于状态函数应用file_exists()和clearstatcache();

Your Ad Here

PHP TIPS:关于状态函数应用

不知道大家注意到没有,在使用PHP的状态函数时,有时会遇到些奇怪的问题。
比如下面程序在使用file_exists()函数时出现的状况:
<?php
$myfile = "/usr/local/somefile.txt";
if(!file_exists($myfile)){
 $fp = fopen($myfile,"w");//创建一个
}
if(!file_exists($myfile)){
 echo "The File exists";
}else{
 echo "The file does not exists";
}
?>
运行后发现,运行结果和下面程序一样,输出结果是:The file does not exists
<?php
$myfile = "/usr/local/somefile.txt";
$exists = file_exists($myfile);
if(!$exists){
 $fp = fopen($myfile,"w");//创建一个
}
if(!$exists){
 echo "The File exists";
}else{
 echo "The file does not exists";
}
?>
查了资料才发现,原来PHP对此类状态函数做了CACHE处理。
由于状态函数在调用时很占内存,调用的结果就被保存在CACHE中以便快速存取。
因此,对于上面程序想要达到预期的结果,必须使用clearstatcache()函数清除cache。
<?php
$myfile = "/usr/local/somefile.txt";
if(!file_exists($myfile)){
 $fp = fopen($myfile,"w");//创建一个
}
clearstatcache();
if(!file_exists($myfile)){
 echo "The File exists";
}else{
 echo "The file does not exists";
}
?>

PS:以上程序在PHP4下调试。
需要调用clearstatcache()的状态函数有:stat(),lstat(),file_exists(),
is_writeable(),is_readable(),is_execntable(),id_file(),is_dir(),
is_link(),filectime(),fileatime(),filemtime(),fileinode(),filegropu(),
fileowner(),filesize(),filetype(),fileperms();


另外,状态函数的结果仅在程序的执行周期内保存在CACHE中,如果对一个特定文件只调用一次状态
函数就没有必要清除cache了。

改变教育制度就能解决就业问题?:【上一篇】
Google的物理构造:【下一篇】
【相关文章】
  • 配置Apache2+PHP5+MYSQL5
  • User Interface Design Tips, Techniques, and Principles
  • php5学习笔记
  • php中几个函数的比较(读手册小总结)
  • PHP的模版技术介绍
  • WINDOWS下php5环境的安装与配置(1)
  • WINDOWS下php5环境的安装与配置(2)
  • 构建安全的apache+mysql+php的web服务器
  • XGL Install and General Tips For Gnome and Nvidia
  • 概率计算 PHP幸运星
  • 【随机文章】
  • Emacs配置gcl,用于开发Common Lisp
  • 不得不看:超实用低级格式化方法
  • 运用反射给实体赋值
  • Linux下 vpn 的配置
  • excel模板检验与合并
  • RIP拓扑变化
  • JAVA写的服务器监视器
  • Eclipse Action 2
  • Tomcat 问题: Cannot serialize session attribute XXX for ..的解决办法
  • 绘制郑州商业银行标志(2)
  • 【相关评论】
    没有相关评论
    【发表评论】
    姓名:
    邮件:
    随机码*
    评论*
          
    |  首 页  |  版权声明  |  联系我们   |  网站地图  |
    CopyRight © 2004-2007 软讯网络 All Rigths Reserved.