要先清除文件状态缓存:
    $year=date("Y",time());
if(!chdir("data/$year")) {
chdir("data");
mkdir($year);//如果不存在,直接建立文件夹
}
clearstatcache();//清除文件状态缓存
chdir("data/$year");
echo getcwd();

解决方案 »

  1.   

    clearstatcache -- Clears file status cache
    Description
    void clearstatcache ( void)
    When you use stat(), lstat(), or any of the other functions listed in the affected functions list (below), PHP caches the information those functions return in order to provide faster performance. However, in certain cases, you may want to clear the cached information. For instance, if the same file is being checked multiple times within a single script, and that file is in danger of being removed or changed during that script's operation, you may elect to clear the status cache. In these cases, you can use the clearstatcache() function to clear the information that PHP caches about a file. 注: This function caches information about specific filenames, so you only need to call clearstatcache() if you are performing multiple operations on the same filename and require the information about that particular file to not be cached. 
      

  2.   

    我查了资料:
    clearstatcache();
    受影响的函数包括 stat(),lstat(),file_exists(),is_writable(),is_readable(),is_executable(),is_file(),is_dir(),is_link(),filectime(),fileatime(),filemtime(),fileinode(),filegroup(),fileowner(),filesize(),filetype() 和 fileperms()。
      

  3.   

    你的代码有问题!!!
    $year=date("Y",time());
    if(! chdir("data/$year")) { //不要用这种方式试探目录是否存在,应用is_dir()或file_exists()检查。至少也要在chdir前面加个@。当然这不是主要问题
    chdir("data"); //注意这里!你以在data目录下了
      mkdir($year);
    }
    chdir("data/$year");//进入data/$year子目录,注意这里。你的data目录下有data/$year字目录吗?你刚才建的只是$year子目录
    echo getcwd();//由于前面的chdir因找不到data/$year子目录而操作失败,自然目录就不会改变了刷新后就能正常显示,是因为目录已经存在,也就没有了chdir("data")的执行。而直接chdir("data/$year")当然也就可以了。
      

  4.   

    搞定了,非常感谢各位感谢唠叨同学!
    其实很简单,是我没搞清函数意义代码如下:$year=date("Y",time());
    if(!is_dir("data/$year")) {
    mkdir("data/$year");//如果不存在,直接建立文件夹
    }
    echo getcwd();