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?
if you need a root propmt, either execute the shell as root, or run sudo -i to run a series of commands as root. second, the root dir is / , not /root. or better yet, use sudo ls .... /root to run your list command as root.
While checking if a file exists, the most commonly used file operators are -e and -f. The '-e' option is used to check whether a file exists regardless of the type, while the '-f' option is used to return true value only if the file is a regular file (not a directory or a device).
access() Function to Check if a File Exists in C Another way to check if the file exists is to use the access() function. The unistd. h header file has a function access to check if the file exists or not. We can use R_OK for reading permission, W_OK for write permission and X_OK to execute permission.
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, i.e. 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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With