首页 > 语言 > PHP > 正文

PHP中is_file()函数使用指南

2024-09-04 11:42:39
字体:大 中 小
来源:转载
供稿:网友

php中is_file()函数是用来判断文件是否存在,使用方法也非常的简单,有需要的小伙伴可以参考下。

is_file() 函数检查指定的文件名是否是正常的文件。

is_file — Tells whether the filename is a regular file

用法:bool is_file ( string $filename ) $file 为必选参数

如果文件存在且为正常的文件则返回 TRUE。

先来看一个实例一:

  1. <?php 
  2. var_dump(is_file('a_file.txt')) . "/n"; 
  3. var_dump(is_file('/usr/bin/')) . "/n"; 
  4. ?> 

上例将输出:

bool(true)

bool(false)

实例二:

  1. <?php 
  2. function isfile($file){ 
  3. return preg_match('/^[^.^:^?^-][^:^?]*.(?i)' . getexts() . '$/',$file); 
  4. //first character cannot be . : ? - subsequent characters can't be a : ? 
  5. //then a . character and must end with one of your extentions 
  6. //getexts() can be replaced with your extentions pattern 
  7. } 
  8. function getexts(){ 
  9. //list acceptable file extensions here 
  10. return '(app|avi|doc|docx|exe|ico|mid|midi|mov|mp3| 
  11. mpg|mpeg|pdf|psd|qt|ra|ram|rm|rtf|txt|wav|word|xls)'; 
  12. } 
  13. echo isfile('/Users/YourUserName/Sites/index.html'); 
  14. ?> 

实例三:

  1. <?php 
  2. function deletefolder($path) 
  3. { 
  4. if ($handle=opendir($path)) 
  5. { 
  6. while (false!==($file=readdir($handle))) 
  7. { 
  8. if ($file<>"." AND $file<>"..") 
  9. { 
  10. if (is_file($path.'/'.$file)) 
  11. { 
  12. @unlink($path.'/'.$file); 
  13. } 
  14. if (is_dir($path.'/'.$file)) 
  15. { 
  16. deletefolder($path.'/'.$file); 
  17. @rmdir($path.'/'.$file); 
  18. } 
  19. } 
  20. } 
  21. } 
  22. } 
  23. ?> 

此函数将删除所有文件与文件夹。

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表