php判断文件函数is_dir is_file is_link is_uploaded_file is_readable等实例讲解

时间:2016-09-27
本文章向大家介绍php判断文件函数的使用实例,包括is_dir判断文件是否为目录、is_file判断是否为文件、is_link判断文件名是否为一个符号连接、is_uploaded_file判断文件是否是通过HTTP POST上传的、is_readable及is_executable判断文件是否可读和可执行。

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_link 

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

语法:

boolean is_link(string filename);

返回值: 布尔值

函数种类: 文件存取

实例:

<?php
$link = 'uploads';

if (is_link($link)) {
    echo(readlink($link));
} else {
    symlink('uploads.php', $link);
}
?>

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_link 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 link)<BR>";
     if(is_link($testfile)) echo "It is a symbolic link 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>";
?>