开发者

file_exists() does not run

开发者 https://www.devze.com 2023-02-13 19:27 出处:网络
$myfilepath =SITEROOT.\"/uploads/vaibhav_photo/thumbnail/\".$user_avatar[\'thumbnail\']; if(file_exists($myfilepath))
$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开发者_如何学JAVA is present.

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


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


$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.


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.


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';
0

精彩评论

暂无评论...
验证码 换一张
取 消