2011-05-27 56 views
1

我正在測試我的網站上的免費幫助臺腳本。經過一些故障排除後,我能夠安裝該腳本,但仍無法正常工作。PHP中的相對路徑不起作用

,我收到的時候我嘗試提交新票的錯誤:

Warning: require(./hesk_settings.inc.php) [function.require]: failed to open stream: No such file or >directory in /home/onesourc/test/hesk/index.php on line 39

Fatal error: require() [function.require]: Failed opening required './hesk_settings.inc.php' (include_path='.:/usr/local/lib/php:/usr/local/Zend/lib/ZendFramework-1.11.6/library') in /home/onesourc/test/hesk/index.php on line 39

我必須在正確的位置hesk_settings.inc.php和相對路徑應該是工作,但事實並非如此。

這裏的代碼片段指出,正試圖找到相對路徑的PHP文件中:

define('HESK_PATH','./'); 
require(HESK_PATH . 'hesk_settings.inc.php'); 

的代碼是從http://www.hesk.com到來。這是一個簡單的安裝,我能夠啓動並運行在本地xampp安裝程序上。

在我的Linux服務器上是否存在導致此問題的設置?一個php.ini設置或其他東西?因爲它是一個php相關的問題,所以我首先想到了問stackoverflow。

如果您需要了解更多信息,請告訴我。 謝謝你的時間!


更新

不知道回答關於此意見的最佳途徑。 我能夠做「ls -la/home/onesourc/test/hesk /」。有太多的子目錄和文件在最後添加星號。它列出了一個很長的列表,我不確定如何輕鬆找回它。

ls -la /home/onesourc/test/hesk/

drwxr-xr-x 9 onesourc onesourc 4096 May 27 18:33 ./ 
drwxr-xr-x 22 onesourc onesourc 4096 May 27 00:01 ../ 
drwxr-xr-x 2 onesourc onesourc 4096 May 27 02:06 admin/ 
drwxrwxrwx 2 onesourc onesourc 4096 May 27 01:43 attachments/ 
-rw-r--r-- 1 onesourc onesourc 3191 May 27 02:06 change_status.php 
-rw-r--r-- 1 onesourc onesourc 3444 May 27 02:06 download_attachment.php 
-rw-r--r-- 1 onesourc onesourc  0 May 27 00:07 footer.txt 
-rw-r--r-- 1 onesourc onesourc 657 May 27 02:06 gd_test.php 
-rw-r--r-- 1 onesourc onesourc  0 May 27 00:07 header.txt 
drwxr-xr-x 2 onesourc onesourc 4096 May 27 00:08 help_files/ 
-rw-r--r-- 1 onesourc onesourc 7394 May 27 02:06 hesk_javascript.js 
-rw-rw-rw- 1 onesourc onesourc 5117 May 27 02:09 hesk_settings.inc.php 
-rw-r--r-- 1 onesourc onesourc 10791 May 27 02:06 hesk_style.css 
drwxr-xr-x 3 onesourc onesourc 4096 May 27 00:04 img/ 
drwxr-xr-x 3 onesourc onesourc 4096 May 27 00:08 inc/ 
-rw-r--r-- 1 onesourc onesourc 32213 May 27 18:33 index.php 
-rw-r--r-- 1 onesourc onesourc 23401 May 27 02:06 knowledgebase.php 
drwxr-xr-x 3 onesourc onesourc 4096 May 27 00:08 language/ 
-rw-r--r-- 1 onesourc onesourc 5843 May 27 02:06 print.php 
-rw-r--r-- 1 onesourc onesourc 2246 May 27 02:06 print_sec_img.php 
-rw-r--r-- 1 onesourc onesourc 3724 May 27 02:06 rate.php 
-rw-r--r-- 1 onesourc onesourc 34057 May 27 02:06 readme.html 
-rw-r--r-- 1 onesourc onesourc 6158 May 27 02:06 reply_ticket.php 
-rw-r--r-- 1 onesourc onesourc 3273 May 27 02:06 secimg.inc.php 
-rw-r--r-- 1 onesourc onesourc 12564 May 27 02:06 submit_ticket.php 
-rw-r--r-- 1 onesourc onesourc 3270 May 27 02:06 suggest_articles.php 
-rw-r--r-- 1 onesourc onesourc 18174 May 27 02:06 ticket.php 
drwxr-xr-x 2 onesourc onesourc 4096 May 27 02:13 tmp/ 
-rw-r--r-- 1 onesourc onesourc 22181 May 27 02:06 TreeMenu.js 
+0

「ls -la/home/onesourc/test/hesk/*」顯示什麼?什麼是權限? – 2011-05-27 22:16:24

+0

是否有'/home/onesourc/test/hesk/hesk_settings.inc.php'?這是PHP沒有找到的文件。 – yitwail 2011-05-27 22:22:46

回答

8

PHP包括路徑是一個位一個棘手的問題的。簡而言之,requireinclude函數可以根據調用者腳本以及定義的PHP包含路徑進行工作,這些路徑可能不是實際的腳本本身。因此,採取例如test.php包括libs/library.php,其中包括functions/helpers/helper.php

test.php的

require_once('libs/library.php'); 

庫/ library.php

require_once('../functions/helpers/helper.php'); 

現在,看着這個代碼,你預計library.php的相對包括工作。不幸的是,這種情況並非如此。實際上,這個要求是相對於test.php的位置,所以你會在你的包含中得到一個致命的錯誤。

解決此問題的一種方法是使用包含文件列出各種常用目錄的絕對路徑。例如:

global。PHP

define("APP_ROOT", "/home/user/site"); 
define("LIB_DIR", APP_ROOT . "/libs"); 
define("FUNCTION_DIR", APP_ROOT . "/functions"); 

現在,因爲路徑是絕對的,我們不會有包括文件的問題:

test.php的

require_once('global.php'); 
require_once(LIB_DIR . '/library.php'); 

library.php

require_once(FUNCTION_DIR . '/helpers/helper.php'); 

另一種選擇,雖然我認爲不太優選的方法,是使用dirname(__FILE__),它給你當前文件,然後可以使用相對於絕對路徑包括:

library.php

require_once(dirname(__FILE__) . '/../functions/helpers/helper.php'); 

這不像上面顯示的常數名稱方法那樣清晰。

+0

謝謝你的迴應!這對我開始寫自己的腳本時會有很大的幫助。 – Torein 2011-05-28 10:36:31

1

如果有人被保持清醒,試圖解決這個問題,我想在我上牀之前發佈我找到的解決方案。

經過許多關於apache和php設置的搜索之後,我終於遇到了有關.htaccess文件的信息。對於那些你不知道的(像我)誰也什麼一個.htaccess是這裏是從apache.org網站的定義:

.htaccess files (or "distributed configuration files") provide a way to make configuration changes on a per-directory basis. A file, containing one or more configuration directives, is placed in a particular document directory, and the directives apply to that directory, and all subdirectories thereof.

在從我的hesk腳本位於其中的父目錄我有一個已經在它這些行.htaccess文件:

Options +FollowSymlinks 
    RewriteEngine on 
    RewriteRule ^filename/([A-Za-z0-9_%\s\.-]+)/([0-9]+)/([0-9]+)/oneSOURCE\.html$ /functions/download2.php?file=$2&filename=$1&key=$3 [L] 
    RewriteCond %{REQUEST_URI} "/userfiles/documents/" 
     RewriteRule (.*) index.html [L] 
    RewriteCond %{REQUEST_URI} "/functions/download2.php" 
     RewriteRule (.*) $1 [L] 
    RewriteCond %{REQUEST_URI} "/functions/databaseDump.php" 
     RewriteRule (.*) $1 [L] 
    RewriteCond %{REQUEST_URI} "/functions/queryResults.php" 
     RewriteRule (.*) $1 [L] 
    RewriteCond %{REQUEST_URI} "/functions/checkForDocs.php" 
     RewriteRule (.*) $1 [L] 
    RewriteCond %{REQUEST_URI} "/reports/" 
     RewriteRule (.*) $1 [L] 
    RewriteCond %{REQUEST_FILENAME} -f 
    RewriteRule \.(php)$ index.php/$1 [L] 
    RewriteRule \.(html)$ index.php/$1 [L] 
    RewriteRule (.*) - [L] 
    RewriteRule (.*) index.php/$1 [L] 

我創建了一個.htaccess文件與它「RewriteEngine敘述關」,並上傳到hesk目錄。現在來自原始腳本的相對路徑工作得很好。

我很抱歉,如果這個問題和答案不屬於stackoverflow。我最初認爲這個問題與PHP和編碼有關,但它似乎與目錄的Apache配置有關。

PS如果有人對我在尋找這個解決方案的路線感到好奇,請隨時在評論中提問。

+0

要走的路。 htaccess不是我的特長。只是好奇,不需要完整的解釋,但簡而言之,htaccess阻止訪問除index.php之外的任何內容? – yitwail 2011-05-29 02:53:52

+0

@yitwail我不這麼認爲。我仍然能夠在腳本中創建各種頁面。只是當腳本需要通過相對路徑訪問任何其他文件時,他們不能工作。 我還沒有花時間來查找RewriteRule和RewriteCond的用途,以及這些情況下的操作。也許當我得到一些空閒時間時,我會看看它。 如果其他人對Apache配置有更多的瞭解,可隨意加入。 – Torein 2011-05-29 03:37:17