问题
I have the following directory structure where the Document Root is pointed to /var/www/html/public:
- /application
- /application/controllers
- /public (Document Root)
I am trying to use the following code to check if a file exists inside a file in the /public directory (working on local setup, but not on live server)
<?php
if (file_exists('../application/controllers/TestController.php'))
{
/* do something */
}
Could there be something in the Apache config file the I need to add? I am not sure where to go from here.
回答1:
file_exists() will work anywhere, it has nothing to do with the document root and there are no apache config settings that can change this.
There are two possible issues that could cause your problem:
- the relative path is not relative to what you think it is. Please use an absolute path and edit your question to show the code you used to check the absolute path (the code you posted in comments had a mistake by the way)
- the UNIX process running PHP does not have permission to access that file. Please check what user PHP is running as and check the permissions of the file.
回答2:
So the real problem that I found was that on my live server, the file name did not change when I renamed it to lowercase (APIController => ApiController). Even though the change happened locally, my GIT commit did not rename the file properly when the code was committed.
Sorry for the confusion!
来源:https://stackoverflow.com/questions/26520615/php-file-exists-not-working-outside-document-root