当前位置:Gxlcms > PHP教程 > php判断是否一个文件的函数is_file()应用举例

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

时间:2021-07-01 10:21:17 帮助过:26人阅读

  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. ?>

人气教程排行