PHP中的相对路径无法在服务器上运行 [英] Relative paths in PHP not working on server

查看:238
本文介绍了PHP中的相对路径无法在服务器上运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

背景信息:
我一直在我自己的服务器(localhost)上开发一个自托管的Facebook应用程序,并且一切正常。最近,我将我的文件上传到我的共享主机在线服务器(hostgator)并弹出了一个奇怪的错误。

Background info: I've been developing a self hosted Facebook app on my own server (localhost) and have everything all working. Recently, I uploaded my files to my shared hosting online server (hostgator) and an odd bug popped up.

我的require_once()函数似乎忽略了hostgator上的相对路径服务器。例如,在index.php中,我有require_once('fb / login.php')。这会引发错误,指出无法找到example.com/login.php中的文件。为什么require()会忽略我路径的fb部分?请记住,这在我的私有localhost服务器上完全正常。
我做了一些谷歌搜索并通过进入php.ini并打开include_path =:解决了一半的问题。这摆脱了第一个错误,但现在login.php也有require_once('fb / sdk / Facebook.php'),无法找到该文件。我知道嵌套需求会变得混乱,但这确实可以在我的localhost服务器上运行。
两台服务器都是PHP5,我确保php.ini文件类似。默认情况下,localhost php.ini已注释include_path。我甚至用我的localhost替换了在线php.ini文件。依然没有。
我尝试过使用绝对路径,它们确实可以工作,但是我的代码中有很多包含和要求,并且不希望每个路径都有代码。任何见解?这是一个非常令人生气的问题,我希望有一个简单的解决方案。

My require_once() functions seem to ignore relative paths on the hostgator server. For example, in index.php, I have require_once('fb/login.php'). This throws an error, stating that the file at example.com/login.php cannot be found. Why does require() ignore the fb part of my path? Remember, this works perfectly fine on my private localhost server. I did some Googling and solved half the problem by going into php.ini and turning on include_path=".:". This got rid of the first error, but now login.php also has require_once('fb/sdk/Facebook.php') and cannot locate that file. I understand that it gets messy with nested requires, but this does work on my localhost server. Both servers are PHP5, and I have made sure that the php.ini files are similar. By default, the localhost php.ini has include_path commented. I have even gone as far to replace the online php.ini file with my localhost one. Still, nothing. I have tried using absolute paths, and they do work, but I have many includes and requires throughout my code and do not want to hand code every single path. Any insight? This is a really infuriating problem and I hope there is an easy solution.

谢谢!

我做了大量研究,最后使用$ _SERVER ['DOCUMENT_ROOT']。'fb / login。 php'作为我所有需求和包含的路径。解决了我所有的问题,感谢上帝

Well I did a ton of research and ended up using $_SERVER['DOCUMENT_ROOT'].'fb/login.php' as my path for all of my requires and includes. Fixed all of my problems, thank god

推荐答案

我很久以前就停止使用相对路径来避免你遇到的麻烦现在经历。考虑像所有PHP框架一样 - 它们定义一个变量来指示服务器根目录的确切位置,然后您可以使用它来构建文件的绝对路径。

I stopped using relative paths a long time ago to avoid the kind of troubles you are going through now. Consider doing like all PHP frameworks do - they define a variable that indicates where exactly is the root of the server, then you can use that to build absolute paths to your files.

您可以通过将此添加到root index.php文件来实现:

You can do that by adding this to you root index.php file:

define('DOCROOT', realpath(dirname(__FILE__)).'/');

然后只需要查看所有 require 语句并首先添加DOCROOT:

Then it's just a matter of going through all your require statements and adding DOCROOT first:

require_once('fb/login.php');

变为:

require_once(DOCROOT . 'fb/login.php');

您甚至可以通过替换 require_once('<进行全局搜索和替换/ code> with require_once(DOCROOT。'

这篇关于PHP中的相对路径无法在服务器上运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆