首頁 >後端開發 >php教程 >php 判斷是否一個檔案的函數is_file()應用舉例

php 判斷是否一個檔案的函數is_file()應用舉例

WBOY
WBOY原創
2016-07-25 09:00:46891瀏覽
  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