Discuz教程网

PHP is_file 判断给定文件名是否为一个正常的文件

[复制链接]
authicon dly 发表于 2011-9-5 16:39:40 | 显示全部楼层 |阅读模式
is_file() 函数检查指定的文件名是否是正常的文件。
is_file — Tells whether the filename is a regular file
用法

bool is_file ( string $filename ) $file 为必选参数
如果文件存在且为正常的文件则返回 TRUE。

来看一个测试is_file经典实例
代码如下:

  1. <?php
  2. var_dump(is_file('a_file.txt')) . "\n";
  3. var_dump(is_file('/usr/bin/')) . "\n";
  4. ?>
复制代码




上例将输出:
bool(true)
bool(false)
用法二
代码如下:

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




实例三
代码如下:

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




此函数删除所有事情,定义的文件夹




上一篇:PHP for 循环语句使用方法详细说明
下一篇:PHP 输出双引号&quot;与单引号''的方
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

1314学习网 ( 浙ICP备10214163号 )

GMT+8, 2025-5-2 22:07

Powered by Discuz! X3.4

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表