Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 709
Mohit Tripathi Avatar asked Jul 14 '12 16:07

Mohit Tripathi


People also ask

How do I find the root of a file?

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.

What is the condition to check whether file exist or not?

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

How do I check if a file exists in C ++?

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.


1 Answers

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.

like image 63
ashein Avatar answered Sep 30 '22 06:09

ashein