简体   繁体   English

从root检查文件是否存在

[英]check file existence from root

I tried the following code to check existence of file in root. 我尝试了以下代码来检查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'] . 我不确定函数file_exist是否检查绝对路径和相对路径,所以我尝试添加ROOT和$_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. 为了代码可移植性,我建议你总是在file_exists()这样的函数中使用绝对路径,否则当你在不同的目录中包含多个文件和/或在CLI模式下运行时,你可能会file_exists()

ROOT constant may be undefined in your code. 您的代码中可能未定义ROOT常量。 Also, $_SERVER['DOCUMENT_ROOT'] in some circumstances cannot be relied on, ie when you use vhost_alias apache module. 此外,在某些情况下,无法依赖$_SERVER['DOCUMENT_ROOT'] ,即使用vhost_alias apache模块时。

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. 函数file_exists()确切地检查您给它的路径。 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. 您可能想要考虑一下您的工作目录究竟是什么。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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