首页 >后端开发 >php教程 >php 判断是否一个文件的函数is_file()应用举例

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

WBOY
WBOY原创
2016-07-25 09:00:46888浏览
  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. ?>
复制代码


声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn