欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

php 判断是否一个文件的函数is_file()应用举例

程序员文章站 2022-05-18 13:09:15
...
  1. var_dump(is_file('a_file.txt')) . "\n";
  2. var_dump(is_file('/usr/bin/')) . "\n";
  3. ?>
复制代码

输出: bool(true) bool(false)

例2:

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

例3:

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