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

How to determine whether the path is a folder or a file in php

青灯夜游
青灯夜游Original
2021-09-26 14:57:362709browse

In PHP, you can use the if statement and the is_dir() and is_file() functions to determine whether the path is a folder or a file. The syntax "if(is_dir($file)){echo "is a folder"; }else if(is_file($file)){echo "is a file"}".

How to determine whether the path is a folder or a file in php

The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer

php determines whether the path is a folder or File

In PHP, you can use the if statement and the is_dir() and is_file() functions to determine whether the path is a folder or a file.

  • The is_dir() function can check whether the specified file is a directory. If the directory exists, this function returns TRUE.

  • The is_file() function can check whether the specified file is a regular file. If the file is a regular file, this function returns TRUE.

Implementation code:

<?php
header("Content-type:text/html;charset=utf-8");
$file = "img/1.jpg";
if (is_dir($file)) {
	echo "指定路径是文件夹";
} else if (is_file($file)) {
	echo "指定路径是文件";
} else {
	echo "指定路径不是常规文件 ";
}
?>

Output result:

How to determine whether the path is a folder or a file in php

If $f# If the value of ##ile is changed to "img", it will output:

How to determine whether the path is a folder or a file in php

Recommended learning: "

PHP Video Tutorial"

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