PHP | filetype()函数

PHP中的filetype()函数是一个内置函数,用于返回指定文件或目录的文件类型。

null

函数的作用是:接受文件名作为参数,成功时返回七种文件类型中的一种,失败时返回False。

filetype()函数的七个可能返回值是:

  • 文件:常规文件
  • 目录:目录
  • 字符:字符专用设备
  • 链接:符号链接
  • fifo:fifo(命名管道)
  • 块:块专用设备
  • 未知:未知文件类型

filetype()函数的结果被缓存,一个名为clearstatcache()的函数用于清除缓存。

语法:

filetype( $filename )

参数: PHP中的filetype()函数只接受一个参数 $filename 。它指定要知道其类型的文件的文件名。

返回值: 成功时返回文件类型,失败时返回False。

错误和例外 :

  1. 对于大于2GB的文件,一些文件系统函数可能会返回意外的结果,因为PHP的整数类型是有符号的,并且许多平台使用32位整数。
  2. 如果出现故障,filetype()函数将发出E_警告。
  3. 如果多次使用filetype()函数,则必须清除缓冲区。
  4. 如果stat调用失败或文件类型未知,filetype()函数将发出E_NOTICE消息。

例如:

Input : filetype("gfg.txt");
Output : file

Input : filetype("documents");
Output : dir

下面的程序演示了filetype()函数。

方案1 :

<?php
// displaying file type using
// filetype() function
echo filetype ( "gfg.txt" );
?>


输出:

file

方案2 :

<?php
// displaying file type using
// filetype() function
$myfile = "documents" ;
echo $myfile . ': ' . filetype ( $myfile );
?>


输出:

documents : dir

参考: http://php.net/manual/en/function.filetype.php

© 版权声明
THE END
喜欢就支持一下吧
点赞7 分享