Home  >  Article  >  Backend Development  >  How to determine whether it is a file or a folder in php

How to determine whether it is a file or a folder in php

王林
王林Original
2020-08-18 16:22:343620browse

php method to determine whether it is a file or a folder: first use the is_dir() function to determine whether the specified file is a folder, and if so, return true; then use the is_file() function to determine whether the specified file is a folder. Regular file, returns true if it is.

How to determine whether it is a file or a folder in php

is_dir() function checks whether the specified file is a directory. If the directory exists, this function returns TRUE.

(Recommended tutorial: php graphic tutorial)

Syntax:

is_dir(file)

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

Grammar:

is_file(file)

(Learning video recommendation: php video tutorial)

Example 1:

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

Output Result:

images is a directory

Example 2:

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

Output:

test.txt is a regular file

The above is the detailed content of How to determine whether it is a file or a folder in php. 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