Is there a difference between the is_file and file_exists function?

Asked

Viewed 2,514 times

2

Actually, there is some difference between the function 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 meaning, since is file is equivalent to "is it a file?" and file_exists is equivant to "file exists?"

If it is a file that exists, it is a file. And if it is a file, it exists.

So what’s the difference between these functions?

  • 3

    is_file I think it’s to make sure it’s not a directory... just see the manual http://php.net/manual/en/function.is-file.php

  • To see if it’s not directory I just use !is_dir($file)

  • Puts, many answers equal! cascade effect

4 answers

5


Yes there are some differences:

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

is_file() The definition of the manual is

Tells whether the Given file is a regular file.

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

According to that answer from Soen, some examples of unregulated files are:

  • Devices
  • Pipes
  • sockets

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

I did 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

Upshot:

     X      |Pasta |Arquivo|Device|Pasta ou arquivo com caracter estranho
is_file     |false |true   |false |false
file_exists |true  |true   |true  |false 
  • Puts! This I didn’t know! When I want to know if a directory exists, I use is_dir. PHP confused!

  • let’s agree: file_exists to know if a directory exists, is very clueless! Very inconsistent!

  • Ta in the documentation =. PHP is not coherent at all. I mean only in confusion :D rs

  • 1

    You think I was a troll in my commentary: If it is a file that exists, it is a file. And if it is a file, it exists.?

  • It’s just that file may also have a broader meaning, of an item any of the file system which can be either a file or a directory. See for example at Wikipedia: "Within this Definition, it is of Paramount importance that the term "file" includes Directories."

  • @Piovezan, I understood your position. It makes sense

  • @Piovezan, I edited the answer, rs had never heard that term 'regular file'.

Show 2 more comments

5

  • +1 This information of symbolic links is important.

1

1

is_file checks if the file is a valid/existing file. But it doesn’t work if you check only directories.

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

return:

bool(true)
bool(false)

On the other hand, file_exists works, also, to verify the existence of directories.

A point to be raised is:

What is expected of a function that does/checks more than one thing? That it is slower compared to others. It would be interesting to test that and see which one does best in its context.

References:

Manual: is_file, file_exists

Browser other questions tagged

You are not signed in. Login or sign up in order to post.