简体   繁体   中英

check file existence from root

I tried the following code to check existence of file in root.

if($res['profile_picture']!="" && file_exists("images/".$res['users_id']."/thumnails/".$res['profile_picture'])){
    $photo_p="images/".$res['users_id']."/thumnails/".$res['profile_picture'];
}

It works only on root directory not sub directory.

I'm not sure whether function file_exist checks for both absolute and relative paths so I tried adding ROOT and $_SERVER['DOCUMENT_ROOT'] . But still it didn't worked out.

Any Help?

For code portability I suggest you always use absolute paths in functions like file_exists() , otherwise you may wind up breaking your head when including multiple files in different directories and/or running in CLI mode.

ROOT constant may be undefined in your code. Also, $_SERVER['DOCUMENT_ROOT'] in some circumstances cannot be relied on, ie when you use vhost_alias apache module.

Generally,

file_exists("{$_SERVER['DOCUMENT_ROOT']}/images/{$res['users_id']}/thumnails/{$res['profile_picture']}")

should work for you.

The function file_exists() checks exactly the path you give to it. There is no fancy magic in there.

Hand it an absolute path and it will check that path. Hand it a relative path and it will check from the current working directory. You might want to think about what actually is your working directory.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM