user2576961
user2576961

Reputation: 415

File exists issues

I'm attempting to try and debug the following code with the file_exists function. I've ran a var_dump on the avatar directory and it always returns as bool(false). I'm not sure why. I tested the code below and it gets to the file exists but it proves the if statement false everytime. Any thoughts? I have looked and the image is in the directory correctly.

$default_avatar = 'default.jpg';
$avatar_directory = base_url() . 'assets/globals/images/avatars/';
if (!is_null($user_data->avatar))
{
    $avatar = $avatar_directory . $user_data->avatar;
    if (file_exists($avatar))
    {
        $user_data->avatar = $avatar_directory . $user_data->avatar;
    }
    else
    {
        $user_data->avatar = $avatar_directory . $default_avatar;
    }
}
else
{
    $user_data->avatar = $default_avatar;
}

Upvotes: 0

Views: 135

Answers (4)

OpenSorceress
OpenSorceress

Reputation: 2014

assuming you meant base_url() to point to the root of your project -

   $file = __DIR__ . "/path/to/file.ext";

   if (file_exists($file)) {

       //...

   }

Or some variation thereof. This also works:

   __DIR__ . "/.."
  • it resolves to the parent directory of __DIR__.

see PHP's magic constants:

http://php.net/manual/en/language.constants.predefined.php

If you are looking for a remote resource - a file not located on your local filesystem - you have to change your php.ini to permit that. And it's probably not a good idea, this is not usually considered safe or secure. At all.

http://php.net/manual/en/features.remote-files.php

And note:

"This function returns FALSE for files inaccessible due to safe mode restrictions. However these files still can be included if they are located in safe_mode_include_dir."

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

-- edited to add relevant information based on a comment from OP.

Upvotes: 1

Adrian Sluyters
Adrian Sluyters

Reputation: 2241

Well this works both CLI and via Apache etc...:

$avatar_directory = substr(str_replace(pathinfo(__FILE__, PATHINFO_BASENAME), '', __FILE__), 0, -1) . '/assets/globals/images/avatars/'

The did returned is the one that the php file itself is in, not the root.

Upvotes: 1

Patrick Evans
Patrick Evans

Reputation: 42736

from the name base_url seems like a function that will get a url like http://www.mysite.com, which will not work for doing local directory functions.

you need something like getcwd, or a full path

getcwd will get the current working directory (the directory where the initial script was executed from):

//If say script.php was exectued from /home/mysite/www
$avatar_directory = getcwd() . '/assets/globals/images/avatars/';

//$avatar_directory would be
/home/mysite/www/assets/globals/images/avatars/

Upvotes: 1

Fu Xu
Fu Xu

Reputation: 766

$default_avatar = 'default.jpg';
$avatar_directory = 'assets/globals/images/avatars/';
if (!is_null($user_data->avatar))
{
    $avatar = $avatar_directory . $user_data->avatar;
    if (file_exists(FCPATH . $avatar))
    {
        $user_data->avatar = base_url() . $avatar_directory . $user_data->avatar;
    }
    else
    {
        $user_data->avatar = base_url() . $avatar_directory . $default_avatar;
    }
}
else
{
    $user_data->avatar = $default_avatar;
}

Upvotes: 1

Related Questions