The difference between Is_file and file_exists and Is_dir in PHP _php tutorial

Source: Internet
Author: User
This article introduces the differences between Is_file and File_exists and Is_dir in PHP, and the friends who need them can refer to them.

Is_file only determine if the file exists;

The code is as follows Copy Code


$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

File_exists determine whether the file exists or whether the directory exists;

The code is as follows Copy Code

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

Input

1

Is_dir determine 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

Look at the manual, although the results of both functions are cached, but the is_file is almost n times faster.

There is one more notable:

In the case of file existence, Is_file is faster n times than file_exists;
If the file does not exist, Is_file is slower than file_exists;
The conclusion is that the File_exits function does not affect speed because the file does exist, but the is_file effect is large.

Test

The code is as follows Copy Code

File exists (current directory)
Is_file:0.4570ms
File_exists:2.0640ms
File exists (absolute path 3 layer/www/hx/a/)
Is_file:0.4909ms
File_exists:3.3500ms
File exists (absolute path 5 layer/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
File does not exist (absolute path 5 layer/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.html www.bkjia.com true http://www.bkjia.com/PHPjc/631588.html techarticle This article introduces the differences between Is_file and File_exists and Is_dir in PHP, and the friends who need them can refer to them. Is_file only determine if the file exists; The code is copied code as follows? PHP $file = ...

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.