2017-04-13 61 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';