php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #78285 file_exists is 30 times slover than is_dir
Submitted: 2019-07-13 20:13 UTC Modified: 2021-12-14 16:16 UTC
Votes:2
Avg. Score:4.0 ± 1.0
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:0 (0.0%)
From: video dot ice dot power at seznam dot cz Assigned:
Status: Verified Package: *Directory/Filesystem functions
PHP Version: 7.3.7 OS: Windows 10
Private report: No CVE-ID: None
View Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
If you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: video dot ice dot power at seznam dot cz
New email:
PHP Version: OS:

 

 [2019-07-13 20:13 UTC] video dot ice dot power at seznam dot cz
Description:
------------
file_exists() is 30 times slover than is_dir()

Test script:
---------------
for ($i = 0; $i < 1000; $i++) { is_dir('some path to directory'); } // 0.5ms

for ($i = 0; $i < 1000; $i++) { file_exists('some path to directory'); } // 15 ms

Expected result:
----------------
The file_exists method should run in about the same time as the is_dir method does.


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2019-07-15 09:49 UTC] cmb@php.net
-Status: Open +Status: Verified -Assigned To: +Assigned To: cmb
 [2019-07-15 09:49 UTC] cmb@php.net
<?php
    $t0 = hrtime(true);
    for ($i = 0; $i < 1000; $i++) {
        is_dir(__DIR__);
    }
    $t1 = hrtime(true);
    for ($i = 0; $i < 1000; $i++) {
        file_exists(__DIR__);
    }
    $t2 = hrtime(true);
    printf("is_dir      took %f ms\n", ($t1 - $t0)/10e6);
    printf("file_exists took %f ms\n", ($t2 - $t1)/10e6);

outputs for me something like:

    is_dir      took 0.051130 ms
    file_exists took 3.395850 ms
 [2019-07-15 17:06 UTC] cmb@php.net
-Type: Bug +Type: Documentation Problem -Assigned To: cmb +Assigned To:
 [2019-07-15 17:06 UTC] cmb@php.net
Actually, it is to be expected that file_exists() is much slower
than is_dir() (when the realpath cache is enabled), because the
former checks whether the file may be accessed, and since this
information is not cached, it is done for each and every call.

Changing to doc problem, since this behavior is not described in
the manual.
 [2020-04-18 07:37 UTC] maggus dot staab at gmail dot com
Maybe we could somehow produce a overview which sorts the often used (and similar) io functions from faster to more expensive ones.

I can think of

is_file
is_dir
file_exists
filemtime
filectime
fileatime
filesize

Etc.
 [2020-10-09 15:58 UTC] bkfake-php at yahoo dot com
why does `is_file()` contain is_readable() functionality?
Separation of concerns

If I want to determine "is file" without regard to file permissions:.

file_exists($file) && !is_dir($file);    // file is a file!
is_file($file);  // file is a file I have permission to access
 [2020-10-09 21:52 UTC] divinity76 at gmail dot com
additionally, the fact that file_exists() returns false when it can see that the file clearly exists, but isn't readable, is a god-damn bug. if people want to know if a file is readable, we have is_readable() for that. i realize that due to BC-breaking, this can only be fixed in a major release like PHP8 or PHP9, though..
 [2021-12-14 16:16 UTC] cmb@php.net
> additionally, the fact that file_exists() returns false when it
> can see that the file clearly exists, but isn't readable, is a
> god-damn bug.

Debatable.  It is done that way, so the the stat and realpath
caches are not affected by file_exists().  Of course, the
implementation could be changed, but that would at least require
discussion on the internals mailing list.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Nov 07 06:01:29 2024 UTC