PHP file_exists not working outside document root

廉价感情. 提交于 2019-12-11 01:09:31

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!