php is_dir 

is_dir — 判断给定文件名是否是一个目录

说明

bool is_dir ( string $filename )

如果文件名存在并且为目录则返回 TRUE。如果 filename 是一个相对路径,则按照当前工作目录检查其相对路径。

Note: 此函数的结果会被缓存。参见 clearstatcache() 以获得更多细节。

例子

<?
var_dump(is_dir(\'a_file.txt\')) . \" \";
var_dump(is_dir(\'bogus_dir/abc\')) . \" \";

var_dump(is_dir(\'..\')); //one dir up
?>

以上例程会输出:

bool(false)
bool(false)
bool(true)

 

php is_file

is_file() 函数检查指定的文件名是否是正常的文件。

如果文件存在且为正常的文件,则返回 true。

语法:

is_file(file)

参数: 

参数 描述
file 必需。规定要检查的文件。

例子

<?php
var_dump(is_file(\'a_file.txt\')) . \"\\n\";
var_dump(is_file(\'/usr/bin/\')) . \"\\n\";
?>

输出:

bool(true)
bool(false)

 

php is_  

is_ 测试文件是否为链接文件。

语法:

boolean is_ (string filename);

返回值: 布尔值

函数种类: 文件存取

实例:

<?php
$  = \'uploads\';

if (is_ ($ )) {
    echo(read ($ ));
} else {
    sym (\'uploads.php\', $ );
}
?>

 

php is_uploaded_file 

is_uploaded_file() 函数判断指定的文件是否是通过 HTTP POST 上传的。

如果 file 所给出的文件是通过 HTTP POST 上传的则返回 TRUE。

该函数可以用于确保恶意的用户无法欺骗脚本去访问本不能访问的文件,例如 /etc/passwd。

这种检查显得格外重要,如果上传的文件有可能会造成对用户或本系统的其他用户显示其内容的话。

语法:

is_uploaded_file(file)

参数: 

参数 描述
file 必需。规定要检查的文件。

实例:

<?php
$file = \"test.txt\";
if(is_uploaded_file($file))
  {
  echo (\"$file is uploaded via HTTP POST\");
  }
else
  {
  echo (\"$file is not uploaded via HTTP POST\");
  }
?>

输出:

test.txt is not uploaded via HTTP POST

 

php is_readable

is_readable() 函数判断指定文件名是否可读。

如果由 file 指定的文件或目录存在并且可读,则返回 TRUE。

语法

is_readable(file)

参数: 

参数 描述
file 必需。规定要检查的文件。

实例:

<?php
$file = \"test.txt\";
if(is_readable($file))
  {
  echo (\"$file is readable\");
  }
else
  {
  echo (\"$file is not readable\");
  }
?>

输出:

test.txt is readable

 

php is_writeable

is_writeable() 函数判断指定的文件是否可写。

如果文件存在并且可写则返回 true。file 参数可以是一个允许进行是否可写检查的目录名。

语法:

is_writeable(file)

参数: 

参数 描述
file 必需。规定要检查的文件。

实例:

<?php
$file = \"test.txt\";
if(is_writeable($file))
  {
  echo (\"$file is writeable\");
  }
else
  {
  echo (\"$file is not writeable\");
  }
?>

输出:

test.txt is writeable

 

php is_executable

is_executable测试文件是否为可执行文件。

语法: 

boolean is_executable(string filename);

返回值: 布尔值

函数种类: 文件存取

实例:

<?php
$file = \'/home/vincent/somefile.sh\';
if (is_executable($file)) {
    echo $file.\' is executable\';
} else {
    echo $file.\' is not executable\';
}
?>

 

综合实例

php判断文件函数is_dir is_file is_ is_uploaded_file is_readable、is_writeable、is_executable实例:

<?php
     $testfile = \"data.txt\";
     if(!file_exists($testfile)) {
          echo \"Error -- $testfile doesn\'t exist!<BR>\";
          exit;
     }
     echo \"What we know about the file $testfile<BR>\";
     if(is_dir($testfile)) echo \"It is a directory.<BR>\";
     if(is_file($testfile)) echo \"It is an actual file (not symbolic  )<BR>\";
     if(is_ ($testfile)) echo \"It is a symbolic   to a real file<BR>\";
     if(is_uploaded_file($testfile)) echo \"It is an uploaded file<BR>\";
     if(is_readable($testfile)) echo \"The file is readable by PHP<BR>\";
     if(is_writeable($testfile)) echo \"The file is writeable by PHP<BR>\";
     if(is_executable($testfile)) echo \"The file is executable by PHP<BR>\";
?>
  
收藏 打印