Home >Backend Development >PHP Tutorial >Example of application of function is_file() in PHP to determine whether a file is

Example of application of function is_file() in PHP to determine whether a file is

WBOY
WBOYOriginal
2016-07-25 09:00:46891browse
  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. ?>
复制代码


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn