user828122
user828122

Reputation: 7

file_exists() does not run

$myfilepath =  SITEROOT."/uploads/vaibhav_photo/thumbnail/".$user_avatar['thumbnail'];
if(file_exists($myfilepath))
{
  echo "file exist";
}
else
{
  echo "file does not exist";
}

It always goes to else part even though file is present.

if anybody have an alternate option for this in PHP please reply as fast as possible,

Upvotes: 1

Views: 338

Answers (4)

aaz
aaz

Reputation: 5196

Check what your working directory is with getcwd().

Your path

example.com/uploads/etc/etc.jpg

is interpreted relative to the current directory. That's likely to be something like

/var/www/example.com

so you ask file_exists() about a file named

/var/www/example.com/example.com/uploads/etc/etc.jpg

You need to either figure out the correct absolute path (add the path containing all site roots in front of SITEROOT) or the correct relative path (relative to the directory your script is in, without a leading /).

tl;dr: try

$myfilepath = 'uploads/etc/etc.jpg';

Upvotes: 0

xzyfer
xzyfer

Reputation: 14135

Pekka is correct that file_exists does not support http protocol.

You can however use file_get_contents

if(file_get_contents($myfilepath)) {
    echo "file exist";
}

By default this pulls back the entire contents of the file. If this is not what you want you can optimise this by adding some flags:

if(file_get_contents($myfilepath, false, null, 0, 1)) {
    echo "file exist";
}

This syntax will return the first character if it exists.

Upvotes: 0

Manish Trivedi
Manish Trivedi

Reputation: 3559

$myfilepath = SITEROOT.DS.'uploads'.DS.'vaibhav_photo'.DS.'thumbnail'.DS.$user_avatar['thumbnail'];

NOTE::SITEROOT have actual root path and DS is constant DIRECTORY_SEPARATOR.

Upvotes: 0

Pekka
Pekka

Reputation: 449505

file_exists works on file paths only. http:// URLs are not supported.

Upvotes: 6

Related Questions