Home  >  Article  >  Backend Development  >  How does php determine whether it is a directory or a file?

How does php determine whether it is a directory or a file?

青灯夜游
青灯夜游Original
2020-07-23 16:38:343102browse

In PHP, you can use the is_dir() and is_file() functions to determine whether it is a directory or a file. The is_dir() function can determine whether it is a directory. If it is a directory, it returns true. The is_file() function can determine whether it is a file. If it is a regular file, it returns true.

How does php determine whether it is a directory or a file?

PHP is_dir() function

is_dir() function checks whether the specified file is a directory. If the directory exists, this function returns TRUE. [Related tutorial recommendations: "PHP Tutorial"]

Note: The results of this function will be cached. Please use clearstatcache() to clear the cache.

Example:

<?php
$file = "images";
if(is_dir($file))
{
echo ("$file是一个目录");
}
else
{
echo ("$file不是一个目录");
}
?>

Output:

images是一个目录

PHP is_file() function

is_file() function checks the specification Whether the file is a regular file. If the file is a regular file, this function returns TRUE.

Example:

<?php
$file = "test.txt";
if(is_file($file))
{
echo ("$file是常规文件");
}
else
{
echo ("$file不是常规文件");
}
?>

Output:

test.txt是常规文件

Recommended learning:PHP programming from entry to master

The above is the detailed content of How does php determine whether it is a directory or a file?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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