PHP: is_readable not working for relative path, works for absolute
I'm moving a site from one server to another. On the old server, my code calls is_readable("filena开发者_运维百科me") and it works. On the new server, it does not work. The files are exactly the same and "filename" is in the same place relative to the calling page.
When I put in an absolute path instead, is_readable returns true as expected. Any suggestions about what the problem could be?
safe_mode is off and open_basedir is not set in my php.ini. I also modified the file permissions, it doesn't work even if I chmod 777 (but that shouldn't matter since it reads properly when using the absolute path).
The servers probably have different configurations causing the current working directory (CWD) not to be the one where the script is being read. Relative paths are always relative to the CWD, not the current executing script.
You can check the CWD by calling getcwd()
or by using realpath()
to resolve the relative path into an absolute one. If the value is incorrect, you will have to either configure the server properly, or set the CWD manually by doing the following:
chdir(dirname(__FILE__));
Try using realpath on you relative path to check if it points to the right file.
You could also use getcwd to check if you're in the correct directory.
Typically, if you pass a relative path to is_readable (or file_exists), it will return false unless the path happens to be relative to the current PHP direct -- View PHP chdir info
精彩评论