Is there any difference between the is_file and file_exists function?

2

Is there any difference between the is_file and file_exists ?

var_dump(file_exists('public/index.php'); // bool(true)

var_dump(is_file('public/index.php')); // bool(true);

Theoretically, the two would have the same sense, since is file is equivalent to "is it file?" and file_exists is equivalent to "file exists?"

If it is a file that exists, it is a file. And if it's a file, it does exist.

So what's the difference between these functions?

    
asked by anonymous 04.09.2015 / 22:42

4 answers

5

Yes there are some differences:

file_exists () checks whether the directory or file exists as an argument.

is_file () The manual definition is

  

Tell whether the given file is a regular file.

     

Informs if the / string passed as argument is a regular file

According to SOen's response , some examples of non-regular files are:

  • devices
  • pipes
  • sockets

Basically what the function does is check if the specified string ( $filename ) is valid or does not contain strange characters positive case checks if $filename represents a file, otherwise returns false .

I made some tests comparing the two functions, the result is table below

Project Structure

Raiz
  foo
    3.txt - Atalho.lnk 
  txt
    1.txt
    abců.txt
    ů.txt
  ů
<?php
   var_dump(is_file('foo')); //pasta
   var_dump(is_file('txt/1.txt')); //arquivo
   var_dump(is_file('C:\')); //device
   var_dump(is_file('txt/abců.txt')); //arquivo com caracter estranho
   var_dump(is_file('txt/ů.txt')); //arquivo com caracter estranho
   var_dump(is_file('txt/ů')); // pasta com caracter estranho

   var_dump(file_exists('foo')); //pasta
   var_dump(file_exists('txt/1.txt')); //arquivo
   var_dump(file_exists('C:\')); // device
   var_dump(file_exists('txt/ů.txt')); //arquivo com caracter estranho
   var_dump(file_exists('txt/abců.txt')); //arquivo com caracter estranho
   var_dump(file_exists('ů'); //pasta com caracter estranho

Result:

     X      |Pasta |Arquivo|Device|Pasta ou arquivo com caracter estranho
is_file     |false |true   |false |false
file_exists |true  |true   |true  |false 
    
04.09.2015 / 22:50
5

The is_file returns true only if the path is from a file (not directory / symlink). file_exists does not make this distinction and returns true if "something" exists in the path.

EDIT (only to supplement)

There are functions equivalent to is_file to check directories and symlinks, these are is_link 1 and is_dir 2 .

  • link
  • link
  • 04.09.2015 / 22:51
    1

    Method documentation:

    file_exists() - Check if a file or directory exists
    is_file() - Informs if the file is a common file (not a directory)

    References:

    link
    link

        
    04.09.2015 / 22:53
    1

    is_file checks whether the file is a valid / existing file. But it does not work if you only check directories.

    var_dump(is_file('a_file.txt')) . "\n";
    var_dump(is_file('/usr/bin/')) . "\n";
    

    Return:

    bool(true)
    bool(false)
    

    In contrast, file_exists also works to check for directories.

    One point to get up is:

    What is expected of a function that does / verifies more than one thing? Let it be slower in comparison to others. It would be interesting to do tests about this and see which works best in context.

    References:

    Manual: is_file , file_exists

        
    04.09.2015 / 22:54