Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

file_exists with relative path not working

Tags:

php

file_exists not working.. Have also tried with realpath.. Same issue

First checking if the file exists.. file_exists returns false but the file is loaded anyway

chdir(__DIR__.'/../..');

$file = 'frontend.php';

echo "$file\n";
if(file_exists($file)){
    echo "File found\n";
}
else{
    echo "File not found\n";
}

require $file;

output

frontend.php
File not found
Contents of frontend.php
like image 388
clarkk Avatar asked Mar 18 '16 11:03

clarkk


1 Answers

As php.net/file_exists says, the file_exists() function requires:

Path to the file or directory.

So try pre-pending with the path of the directory:

if (file_exists(dirname(__FILE__) . $file)) {
    echo "File found\n";
}
like image 158
Egg Avatar answered Sep 19 '22 23:09

Egg