Cuberonix
Cuberonix

Reputation: 33

PHP: file_exists() not working properly.

file_exists() does not seem to be working properly for me. It defaults right to the else statement, blank_user.png. But it does display without the file_exists() check in the last img in my code. I can't figure out what's going wrong.

<?php 
    $filename = "/project/images/user_image/" . $username . ".png";
    if(file_exists($filename)){ ?>
    <img src = "<?php echo '/project/images/user_image/' . $username . '.png';?>" alt = "User Pic" height = 280 width = 280 />

<?  
    } else {  ?>
    <img src = "<?php echo '/project/images/user_image/default/blank_user.png';?>" alt = "User Pic" height = 280 width = 280 />
<? } ?>
</br>
    <img src = "<?php echo '/project/images/user_image/' . $username . '.png';?>" alt = "User Pic" height = 280 width = 280 />
?>

Upvotes: 1

Views: 496

Answers (1)

Jimbo
Jimbo

Reputation: 26624

Note that file_exists() takes an absolute path. It is not broken for you, you are just passing an incorrect path that does not exist. What you are looking for is a relative path.

In your current solution, you are looking for /project in the root of the filesystem, which I almost guarantee you don't want.

To use a relative path instead, you will need to use __DIR__ . '/../relative/path/here', where __DIR__ is the currently executing PHP file's directory.

Upvotes: 1

Related Questions