跟我学习php文件和目录常用函数-上篇

WBOY
Release: 2016-06-23 13:02:15
Original
1193 people have browsed it

1> bool file_exists ( string $filename )

检查文件或目录是否存在

2> int filesize ( string $filename )

取得指定文件的大小

3> string filetype ( string $filename )

返回文件的类型。

  • 返回值, 可能的值有 fifo,char,dir,block,link,file 和 unknown。
  • 对于windows有'file'、'dir'、或 'unknown'

4> array stat ( string $filename )

获取文件的相关信息

  • 返回文件信息说明
数字下标 关联键名 解释
0 dev 设备名
1 ino 号码
2 mode inode保护模式
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 所占据块的数目

5> string basename ( string $path [, string $suffix ] )

从路径中获取文件的基本文件名

  • $suffix, 如果你写了文件名的后缀返回回来的文件名不包含后缀
$path = 'f/a.txt';echo basename($path,'.txt');//输出: a
Copy after login

6> string dirname ( string $path )

返回字符串中的文件夹路径

$path = 'f1/f2/a.txt';echo dirname($path);//输出: f1/f2
Copy after login

7> mixed pathinfo ( string $path [, int $options = PATHINFO_DIRNAME | PATHINFO_BASENAME | PATHINFO_EXTENSION PATHINFO_FILENAME ] )

返回路径的关联数组或指定的信息

  • $options, 指定输出路径信息

  • 关联数组信息

    $img_info = pathinfo('1.jpg');print_r( $img_info );/*输出:Array([dirname] => .  目录路径,点表示当前路径[basename] => 1.jpg 带扩展名文件名[extension] => jpg 扩展名[filename] => 1 文件名)*/
    Copy after login

8> resource opendir ( string $path [, resource $context ] )

打开一个目录句柄,可用于之后的 closedir(),readdir() 和 rewinddir() 调用中。

9> string readdir ([ resource $dir_handle ] )

返回目录中下一个文件的文件名。文件名以在文件系统中的排序返回。

  • $dir_handle, 使用opendir函数返回的目录句柄

10> int filemtime ( string $filename )

获取文件最后修改时间戳


简单综合实例

header('content-type: text/html;charset=utf-8;');$path = 'folder'; //目录路径$dir_handle = opendir($path);//打开目录echo '<table border="1"> <tr><th>文件名</th><th>类型</th><th>大小</th><th>修改时间</th></tr>';//循环获取目录下的文件while($filename = readdir($dir_handle)){      $filepath = $path.'/'.$filename;//文件的具体路径       //只获取文件类型       if( ($filetype = filetype($filepath) ) == 'file'){          $filesize = filesize($filepath); //文件大小        $filemtime = date("Y/n/t", filemtime($filepath) ); //最后修改时间          //以表格形式输出     echo "<tr>        <td>{$filename}</td><td>{$filetype}</td><td>{$filesize}</td><td>{$filemtime}</td>                   </tr> ";        }}echo '</table>';
Copy after login
  • 运行结果

2016-06-08_202245.png

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!