amitakartok
amitakartok

Reputation: 105

PHP file_exists returns true on nonexistent file

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.

Upvotes: 6

Views: 6344

Answers (3)

Alex
Alex

Reputation: 521

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

Upvotes: 8

amitakartok
amitakartok

Reputation: 105

Found the source of the problem. For whoever might stumble upon this in the future, the Apache that comes with XAMPP doesn't execute PHP code if it's prefixed with <? instead of <?php.

Upvotes: 0

Anas Zilali
Anas Zilali

Reputation: 21

the probleme is in the redirect() function . there is no redirect in php use this code

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

Upvotes: -1

Related Questions