Moh*_*hit 5 php file-exists document-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'];
}
Run Code Online (Sandbox Code Playgroud)
它仅适用于根目录,不适用于子目录。
我不确定函数file_exist是否同时检查绝对路径和相对路径,所以我尝试添加ROOT和$_SERVER['DOCUMENT_ROOT']。但是仍然没有解决。
有帮助吗?
小智 4
为了代码的可移植性,我建议您始终在 等函数中使用绝对路径file_exists(),否则当在不同目录中包含多个文件和/或在 CLI 模式下运行时,您可能会头疼。
ROOT您的代码中可能未定义常量。另外,$_SERVER['DOCUMENT_ROOT']在某些情况下不能依赖,即当您使用vhost_alias apache module.
一般来说,
file_exists("{$_SERVER['DOCUMENT_ROOT']}/images/{$res['users_id']}/thumnails/{$res['profile_picture']}")
Run Code Online (Sandbox Code Playgroud)
应该为你工作。