Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP file_exists returns true on nonexistent file

Tags:

php

I have the following code snippet:

<?
include("/classes/functions.php");
if(!file_exists("/config.ini")) redirect("/classes/install.php");
else{
    //file is processed
}
?>

What this is supposed to do is to read data from a configuration file then use it to connect to a MySQL server. If the configuration file doesn't exist, it redirects to a setup page where the file is created and filled with user-provided data.

Problem is, even though the file doesn't exist, file_exists returns true anyway, which causes the else branch to run and fail all over the place.

I tried using $_SERVER['DOCUMENT_ROOT'] in the file path, just in case; no difference.

like image 739
amitakartok Avatar asked Jul 26 '16 18:07

amitakartok


1 Answers

Documentation: http://php.net/manual/en/function.file-exists.php

Returns TRUE if the file or directory specified by filename exists;

file_exists('/non-existing-file.ini') returns true because path '/' exists

use is_file() instead

like image 186
Alex Avatar answered Oct 05 '22 22:10

Alex