Home > Backend Development > PHP Tutorial > The difference between is_file, file_exists and is_dir in php_PHP tutorial

The difference between is_file, file_exists and is_dir in php_PHP tutorial

WBOY
Release: 2016-07-13 16:56:36
Original
1056 people have browsed it

This article introduces the difference between is_file, file_exists and is_dir in php. Friends in need can refer to it.

is_file only determines whether the file exists;

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
The code is as follows
 代码如下 复制代码


$file = "test.txt";   
if(is_file($file)) {   
echo ("$file is a regular file");   
}else {   
echo ("$file is not a regular file");   
}   
?>   

输出:test.txt is a regular file

Copy code

 代码如下 复制代码

echo file_exists("test.txt");
?>

输入

1

$file = "test.txt";

if(is_file($file)) {  

echo ("$file is a regular file");
代码如下 复制代码

$file = "images";
if(is_dir($file))
{
echo ("$file is a directory");
}
else
{
echo ("$file is not a directory");
}
?>

输出:

images is a directory

}else {  

echo ("$file is not a regular file");

}  

?>  

Output: test.txt is a regular file

file_exists determines whether the file or directory exists;
 代码如下 复制代码

文件存在(当前目录)
is_file:0.4570ms
file_exists:2.0640ms
文件存在(绝对路径3层/www/hx/a/)
is_file:0.4909ms
file_exists:3.3500ms
文件存在(绝对路径5层/www/hx/a/b/c/)
is_file:0.4961ms
file_exists:4.2100ms
文件不存在(当前目录)
is_file:2.0170ms
file_exists:1.9848ms
文件不存在(绝对路径5层/www/hx/a/b/c/)
is_file:4.1909ms
file_exists:4.1502ms
目录存在
file_exists:2.9271ms
is_dir:0.4601ms
目录不存在
file_exists:2.9719ms
is_dir:2.9359ms

The code is as follows Copy code
echo file_exists("test.txt");
?>Enter 1 is_dir determines whether the directory exists; Example
The code is as follows Copy code
$file = "images";<🎜> if(is_dir($file))<🎜> {<🎜> echo ("$file is a directory");<🎜> }<🎜> else<🎜> {<🎜> echo ("$file is not a directory");<🎜> }<🎜> ?> Output: images is a directory Looking at the manual, although the results of these two functions will be cached, is_file is N times faster. One more thing worth noting: When the file exists, is_file is N times faster than file_exists; When the file does not exist, is_file is slower than file_exists; The conclusion is that the file_exits function does not affect the speed depending on whether the file actually exists, but the impact of is_file is greater Test
The code is as follows Copy code
File exists (current directory) is_file:0.4570ms file_exists:2.0640ms File exists (absolute path level 3/www/hx/a/) is_file:0.4909ms file_exists:3.3500ms The file exists (absolute path level 5/www/hx/a/b/c/) is_file:0.4961ms file_exists:4.2100ms File does not exist (current directory) is_file:2.0170ms file_exists:1.9848ms The file does not exist (absolute path level 5/www/hx/a/b/c/) is_file:4.1909ms file_exists:4.1502ms Directory exists file_exists:2.9271ms is_dir:0.4601ms Directory does not exist file_exists:2.9719ms is_dir:2.9359ms http://www.bkjia.com/PHPjc/631588.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/631588.htmlTechArticleThis article introduces the difference between is_file, file_exists and is_dir in php. Friends in need can refer to it. is_file only determines whether the file exists; the code is as follows Copy the code ?php $file =...