🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
DirectoryIterator 个更多参考[文件系统(核心)](https://www.kancloud.cn/a173512/php_note/1690598) ## **文件信息函数** | 函数名 | 函数提供什么文件信息? | | --- | --- | | file_exists() | 该文件或目录是否存在(当文件) | | is_dir() | 判断给定文件名是否是一个目录 | | is_file() | 判断给定文件名是否为一个正常的文件 | | is_link() | 判断给定文件名是否为一个符号连接 | | is_readable() | 判断给定文件名是否可读 | | is_writable() | 判断给定文件名是否可写 | | is_executable() | 判断给定文件名是否可执行(自PHP5.0.0起可用于Windows) | | fileperms() | 取得文件的权限 | | filesize() | 取得文件大小的字节数 | | filetype() | 取得文件类型,可能返回 fifo,char,dir,block,link,file 和 unknown | | fileatime() | 最后访问时间 | | filectime() | 文件inode的最后修改时间 | | filegroup() | 取得文件组(返回整数) | | fileinode() | 取得文件的信息节点数(返回整数) | | filemtime() | 取得文件数据块最后被写入的时间(返回Unix时间戳) | | fileowner() | 取得文件的所有者(返回用户ID) | ## **目录相关函数** | 函数名 | 函数提供什么文件信息? | | --- | --- | | mkdir() | 创建一个新目录,第二个参数可用来设置访问权限 | | rmdir() | 删除目录 | | rename() | 重命名一个文件或目录 | ## **目录类相关方法** [DirectoryIterator](http://cn.php.net/manual/zh/class.directoryiterator.php)类封装了很多与目录相关的方法 | 方法名 | 函数提供什么目录信息? | | --- | --- | | isDir() | 判断给定的DirectoryIterator item对象是否是一个目录 | | isDot() | 判断当前的DirectoryIterator item对象是否是‘.’或‘..’ | | isFile() | 判断当前的DirectoryIterator item对象是否是一个有效的文件 | | isLink() | 判断当前的DirectoryIterator item对象是否是一个连接 | | isReadable() | 判断当前的DirectoryIterator item对象是否可读 | | isWritable() | 判断当前的DirectoryIterator item对象是否可写 | | isExecutable() | 判断当前的DirectoryIterator item对象是否可执行 | | getATime() | 获取当前Iterator item最后访问时间 | | getCTime() | 获取当前Iterator item最后修改时间 | | getMTime() | 获取当前Iterator item文件数据块最后被写入的时间 | | getFilename() | 获取当前Iterator item文件名(带扩展名) | | getPathname() | 获取当前Iterator item路径名 | | getPath() | 获取当前Iterator item路径名和文件名 | | getGroup() | 获取当前Iterator item组ID | | getOwner() | 获取当前Iterator item拥有者ID | | getPerms() | 获取当前Iterator item权限 | | getSize() | 获取当前Iterator item文件大小 | | getType() | 获取当前Iterator item类型,可能是file,link or dir | | getInode() | 获取当前Iterator item的inode节点号 | ## **文件时间戳解惑** [touch()](http://cn.php.net/manual/en/function.touch.php)函数修改文件的更新时间 [fileatime()](http://cn.php.net/manual/en/function.fileatime.php)函数返回文件因为读或写被打开的最后时间 [filemtime()](http://cn.php.net/manual/en/function.filemtime.php)函数返回文件内容被修改的最后时间 [filectime()](http://cn.php.net/manual/en/function.filectime.php)函数返回文件内容或元数据被修改的最后时间 ##** 获取文件信息** 通过stat()可以获取一个包含文件相关信息的数组,与此函数类似的是fstat()函数,这个函数以一个文件句柄作为参数(由fopen()或popen()返回),lstat()用来获取符号或文件连接的信息。 | 数字索引 | 字符串索引 | 说明 | | --- | --- | --- | | 0 | dev | 设备号 | | 1 | ino | 信息节点号 | | 2 | mode | 保护模式 | | 3 | nlink | 被连接数目 | | 4 | uid | 所有者用户ID | | 5 | gid | 所在组ID | | 6 | rdev | 设备类型,如果是inode设备的话 | | 7 | size | 文件大小的字节数 | | 8 | atime | 上次访问的时间(Unix时间戳) | | 9 | mtime | 上次修改的时间(Unix时间戳) | | 10 | ctime | 上次改变的时间(Unix时间戳) | | 11 | blksize | 文件系统IO的块大小 | | 12 | blocks | 所占据块的数目 | ## **修改文件权限** [chmod()](http://cn.php.net/manual/en/function.chmod.php)函数修改文件的权限 [chown()](http://cn.php.net/manual/en/function.chown.php)函数修改文件的所有者 [chgrp()](http://cn.php.net/manual/en/function.chgrp.php)函数修改文件所属组 注意:上述3个函数在Windows系统中无效 ## **获取文件名各部分信息** [**basename**()](http://cn.php.net/manual/en/function.basename.php)函数可以取得文件名,文件不存在也会解析 ``` $file="/home/wwwroot/runtime1/token.txt"; $filename=basename($file); echo $filename;//token.txt //去掉后缀的文件名 echo basename($file,'.txt');//token ``` [**dirname**()](http://cn.php.net/manual/en/function.dirname.php)函数可以取得路径名,文件不存在也会解析 ``` $file="/home/wwwroot/runtime1/token.txt"; echo dirname($file); // /home/wwwroot/runtime1 //常通过dirname(__FILE__)的组合来获取当前目录路径(物理路径,常用在引用其他PHP文件上) echo dirname(__FILE__);// /home/wwwroot/app/controller ``` [**pathinfo**()](http://cn.php.net/manual/en/function.pathinfo.php)取得目录名、完整文件名、扩展名、文件名(即不带扩展名)的关联数组,键名分别是[dirname]、[basename]、[extension]、[filename],文件不存在也会解析 ``` $file="/home/wwwroot/runtime1/token.txt"; var_export(pathinfo($file)); 结果: array ( 'dirname' => '/home/wwwroot/runtime1', 'basename' => 'token.txt', 'extension' => 'txt', 'filename' => 'token', ) ``` ## **删除文件** 用[unlink()](http://cn.php.net/manual/en/function.unlink.php)函数可以删除一个文件,如果删除失败将产生一个E_WARNING错误 提示:PHP5.0.0后此函数也可以用来删除远程文件,如FTP等 ## **复制或移动文件** 使用copy(old_dir,new_dir)函数可以复制文件,使用rename(old_dir,new_dir)移动文件,这里的new_dir是可以可以重命名文件名的。