2017-04-13 34 views
0

这是一个愚蠢的问题,但因为我不能理清,我认为这可能是一件好事,得到一些帮助。问题是,返回目录的“.. /”不起作用。../上的网址在PHP不工作

我正在执行的文件位于主路由上的一个文件夹中,我需要返回主路由,然后进入另一个文件夹来加载这个其他PHP文件,但它不工作可能会导致此问题。

错误:

Warning: require_once(../PHPMailer/PHPMailerAutoload.php): failed to open stream: No such file or directory in things/public_html/classes/Mail.php on line 3

Fatal error: require_once(): Failed opening required '../PHPMailer/PHPMailerAutoload.php' (include_path='.:/opt/alt/php71/usr/share/pear') in things/public_html/classes/Mail.php on line 3

目录结构:

文件,其中requiere曾经是:它想要

/public_html/classes/filethatwantstoacces.php

文件获得:

/public_html/PHPMailer/PHPMailerAutoload.php

require_once('../PHPMailer/PHPMailerAutoload.php'); 
+1

您可以显示一个目录结构图,显示主脚本和'PHPMailerAutoload.php'脚本的位置? – Barmar

+0

@Barmar补充! – Itsmoloco

+0

我不明白为什么它不起作用的任何原因...你可以发布文件的完整代码。另外,你有什么错误? – TricksfortheWeb

回答

1

你应该使用的是$_SERVER['DOCUMENT_ROOT']变量。详情请阅读this answer to another question

If you are using PHP you should get into a habit of NOT using relative file paths at all but to use absolute paths, which will guarentee to succeed every time (As long as the target file exists and is reachable, etc.).

so; use $_SERVER['DOCUMENT_ROOT']

As a side note, you do not need to use brackets for your include s/ require s, it's simply giving the server more work to do for no extra benefit.

The $_SERVER['DOCUMENT_ROOT'] is the base directory of your PHP/web application, typically the contents of the folder /public_html .

使用正确的语法和上面$_SERVER值(这将指向/的public_html文件夹,您将有:

require_once $_SERVER['DOCUMENT_ROOT'].'/PHPMailer/PHPMailerAutoload.php'; 

这会从任何脚本的目录结构中,如果工作,文件(PHPMailerAutoload.php)存在,在给定的位置

0

可达鉴于你的位置

/public_html/classes/filethatwantstoacces.php 

../给你

/public_html/classes 

所以../PHPMailer/PHPMailerAutoload.php计算为

/public_html/classes/PHPMailer/PHPMailerAutoload.php 

由于@Martin指出,使用$_SERVER['DOCUMENT_ROOT']来构建你的文件的绝对路径是最简单的方式,以避免相对目录导航错误如下:

require_once $_SERVER['DOCUMENT_ROOT'].'/PHPMailer/PHPMailerAutoload.php';