Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/258.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/visual-studio-code/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP中的相对路径不工作时出现问题_Php - Fatal编程技术网

PHP中的相对路径不工作时出现问题

PHP中的相对路径不工作时出现问题,php,Php,我正在我的网站上测试一个免费的帮助台脚本。我能够得到一些故障排除后安装脚本,但它仍然不工作 我尝试提交新票证时收到的错误是: 警告:require(./hesk_settings.inc.php)[function.require]:打开流失败:第39行的/home/onesourc/test/hesk/index.php中没有这样的文件或>目录 致命错误:require()[function.require]:在/home/onesourc/test/hesk/index.php第39行的/h

我正在我的网站上测试一个免费的帮助台脚本。我能够得到一些故障排除后安装脚本,但它仍然不工作

我尝试提交新票证时收到的错误是:

警告:require(./hesk_settings.inc.php)[function.require]:打开流失败:第39行的/home/onesourc/test/hesk/index.php中没有这样的文件或>目录

致命错误:require()[function.require]:在/home/onesourc/test/hesk/index.php第39行的/home/onesourc/test/hesk/index.php中打开require()[function.require]:失败

我确实将hesk_settings.inc.php放在了正确的位置,并且相关路径应该可以正常工作,但它们不能正常工作

以下是试图找到相对路径的php文件的代码片段:

define('HESK_PATH','./');
require(HESK_PATH . 'hesk_settings.inc.php');
代码来自。这是一个简单的安装,我可以在我的本地xampp安装程序上安装并运行它

我的Linux服务器上是否存在导致此问题的设置?是php.ini设置还是其他?我想先问stackoverflow,因为这是一个与php相关的问题

如果您需要更多信息,请告诉我。 谢谢你抽出时间


更新

不确定在这里回答评论的最佳方式。 我能做“ls-la/home/onesourc/test/hesk/”。子目录和文件太多,无法在末尾添加星号。它列出了一个很长的列表,我不知道如何轻松地检索它

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


PHP包含路径有点棘手。简而言之,
require
include
函数从调用方脚本以及定义的PHP include路径工作,这些路径可能不是实际的脚本本身。以
test.php
为例,其中包括
libs/library.php
,其中包括
函数/helpers/helper.php

test.php

require_once('libs/library.php');
require_once('../functions/helpers/helper.php');
define("APP_ROOT", "/home/user/site");
define("LIB_DIR", APP_ROOT . "/libs");
define("FUNCTION_DIR", APP_ROOT . "/functions");
require_once('global.php');
require_once(LIB_DIR . '/library.php');
require_once(FUNCTION_DIR . '/helpers/helper.php');
require_once(dirname(__FILE__) . '/../functions/helpers/helper.php');
libs/library.php

require_once('libs/library.php');
require_once('../functions/helpers/helper.php');
define("APP_ROOT", "/home/user/site");
define("LIB_DIR", APP_ROOT . "/libs");
define("FUNCTION_DIR", APP_ROOT . "/functions");
require_once('global.php');
require_once(LIB_DIR . '/library.php');
require_once(FUNCTION_DIR . '/helpers/helper.php');
require_once(dirname(__FILE__) . '/../functions/helpers/helper.php');
现在,看看这段代码,您希望
library.php
的相对include能够工作。不幸的是,情况并非如此。实际上,require将与test.php的位置相关,因此在include中会出现致命错误

解决这个问题的一种方法是使用一个包含文件,其中列出了到各种常用目录的绝对路径。例如:

global.php

require_once('libs/library.php');
require_once('../functions/helpers/helper.php');
define("APP_ROOT", "/home/user/site");
define("LIB_DIR", APP_ROOT . "/libs");
define("FUNCTION_DIR", APP_ROOT . "/functions");
require_once('global.php');
require_once(LIB_DIR . '/library.php');
require_once(FUNCTION_DIR . '/helpers/helper.php');
require_once(dirname(__FILE__) . '/../functions/helpers/helper.php');
现在,由于路径是绝对路径,因此包含文件不会有问题:

test.php

require_once('libs/library.php');
require_once('../functions/helpers/helper.php');
define("APP_ROOT", "/home/user/site");
define("LIB_DIR", APP_ROOT . "/libs");
define("FUNCTION_DIR", APP_ROOT . "/functions");
require_once('global.php');
require_once(LIB_DIR . '/library.php');
require_once(FUNCTION_DIR . '/helpers/helper.php');
require_once(dirname(__FILE__) . '/../functions/helpers/helper.php');
library.php

require_once('libs/library.php');
require_once('../functions/helpers/helper.php');
define("APP_ROOT", "/home/user/site");
define("LIB_DIR", APP_ROOT . "/libs");
define("FUNCTION_DIR", APP_ROOT . "/functions");
require_once('global.php');
require_once(LIB_DIR . '/library.php');
require_once(FUNCTION_DIR . '/helpers/helper.php');
require_once(dirname(__FILE__) . '/../functions/helpers/helper.php');
另一种替代方法,尽管我认为不是首选的方法,是使用
dirname(\uuuuu FILE\uuuu)
,它为您提供当前文件的绝对路径,然后您可以使用该路径进行相对包含:

library.php

require_once('libs/library.php');
require_once('../functions/helpers/helper.php');
define("APP_ROOT", "/home/user/site");
define("LIB_DIR", APP_ROOT . "/libs");
define("FUNCTION_DIR", APP_ROOT . "/functions");
require_once('global.php');
require_once(LIB_DIR . '/library.php');
require_once(FUNCTION_DIR . '/helpers/helper.php');
require_once(dirname(__FILE__) . '/../functions/helpers/helper.php');

这并不像上面所示的常量名称方法那样清晰。

如果有人一直醒着试图为我解决这个问题,我想在睡觉前发布我找到的解决方案

在对apache和php设置进行了大量搜索之后,我终于找到了有关.htaccess文件的信息。对于那些不知道(像我一样)什么是.htaccess的人,这里是apache.org网站的定义:

.htaccess文件(或“分布式配置文件”)提供了按目录进行配置更改的方法。包含一个或多个配置指令的文件放在特定的文档目录中,这些指令应用于该目录及其所有子目录

在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]
我创建了一个包含“RewriteEngine off”的.htaccess文件,并上传到hesk目录。现在,来自原始脚本的相对路径工作正常

如果这个问题和答案不属于stackoverflow,我深表歉意。我最初认为问题与php和编码有关,但似乎与目录的apache配置有关


PS如果有人对我寻找此解决方案的路线感到好奇,请在评论中提问。

ls-la/home/onesourc/test/hesk/*显示了什么?权限是什么?是否有“/home/onesourc/test/hesk/hesk_settings.inc.php”?这是PHP找不到的文件。感谢您的回复!当我开始编写自己的脚本时,这将非常有帮助。这不是我的专长。只是好奇,不需要详细解释,但简而言之,htaccess阻止了对index.php以外的任何东西的访问?@yitwail我不这么认为。我仍然能够在脚本中显示各种页面。当脚本需要通过相对路径访问任何其他文件时,它们不起作用。我还没有花时间查找RewriteRule和RewriteCond的确切用途以及它们在本例中的作用。也许等我有空的时候我会看看。如果其他人更了解apache配置,请随时加入。