您的位置 首页 php

PHP 文件判断操作

1.判断文件或者目录是否存在

定义和用法

file_exists() 函数检查文件或目录是否存在。

如果指定的文件或目录存在则返回 true,否则返回 false。

语法

file_exists(path)

参数描述

path必需。规定要检查的路径。

例: file _exists.php

<?php

$filename = ‘file.txt’;//文件名

fun_file_exists($filename);//调用fun_file_exists()函数

$filename = ‘file2.txt’;//文件名

fun_file_exists($filename);//调用fun_file_exists()函数

function fun_file_exists($filename)//创建函数 形参 $filename

{

if (file_exists($filename)) //如果指定的文件或目录存在则返回 true,否则返回 false。

{

echo “文件名:{$filename}<br><br>”;

}

else

{

echo “{$filename}不存在<br><br>”;

}

}

?>

结果:

2.判断文件是否可读写

1)is_writable

定义和用法

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

语法

is_writable(file)

参数描述

file必需。规定要检查的文件。

说明

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

提示和注释

注释:本函数的结果会被 缓存 。请使用 clearstatcache() 来清除缓存。

例:is_writable.php

<?php

$filename = ‘file.txt’; //文件

if (is_writable($filename)) //如果文件存在并且可写则返回 true。

{

echo “{$filename}可写”;

}

else

{

echo “{$filename}不可写”;

}

?>

结果:

2)is_readable

定义和用法

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

语法

is_readable(file)

参数描述

file必需。规定要检查的文件。

说明

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

提示和注释

注释:本函数的结果会被缓存。请使用 clearstatcache() 来清除缓存。

例:is_readable.php

<?php

$filename = ‘file.txt’;//文件

if (is_readable($filename)) //如果由 file 指定的文件或目录存在并且可读,则返回 TRUE。

{

echo “{$filename}可读”;

}

else

{

echo “{$filename}不可读”;

}

?>

结果:

文章来源:智云一二三科技

文章标题:PHP 文件判断操作

文章地址:https://www.zhihuclub.com/77578.shtml

关于作者: 智云科技

热门文章

网站地图