Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sqlite/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,我正在尝试获取包含PHP脚本的目录的路径。让我们把它命名为script.php。它还包含包含的log.php script.php: include_once __DIR__.'/../lib/log.php'; func(); function func() { file_put_contents(__DIR__.'/log', 'Log file successfully created'); } include_once __DIR__.'/../lib/log.php'; fu

我正在尝试获取包含PHP脚本的目录的路径。让我们把它命名为script.php。它还包含包含的log.php

script.php:

include_once __DIR__.'/../lib/log.php';
func();
function func() {
   file_put_contents(__DIR__.'/log', 'Log file successfully created');
}
include_once __DIR__.'/../lib/log.php';
func(__DIR__);
function func($arg) {
   file_put_contents($arg.'/log', 'Log file successfully created');
}
log.php:

include_once __DIR__.'/../lib/log.php';
func();
function func() {
   file_put_contents(__DIR__.'/log', 'Log file successfully created');
}
include_once __DIR__.'/../lib/log.php';
func(__DIR__);
function func($arg) {
   file_put_contents($arg.'/log', 'Log file successfully created');
}
我执行script.php时希望日志文件与script.php一起出现,但没有出现。相反,将在lib目录中创建log文件以及log.php文件。
我在这里看到的唯一解决方法是通过函数将dir path作为参数传递给log.php,如下所示:

script.php:

include_once __DIR__.'/../lib/log.php';
func();
function func() {
   file_put_contents(__DIR__.'/log', 'Log file successfully created');
}
include_once __DIR__.'/../lib/log.php';
func(__DIR__);
function func($arg) {
   file_put_contents($arg.'/log', 'Log file successfully created');
}
log.php:

include_once __DIR__.'/../lib/log.php';
func();
function func() {
   file_put_contents(__DIR__.'/log', 'Log file successfully created');
}
include_once __DIR__.'/../lib/log.php';
func(__DIR__);
function func($arg) {
   file_put_contents($arg.'/log', 'Log file successfully created');
}
实际上是否可以通过log.php获得script.php的路径而不传递任何参数?

我只想包含脚本,调用函数并创建新文件,以及包含include的脚本。

否。除了与当前正在执行的文件相关的元数据外,PHP不提供任何元数据

要使文件路径更容易处理,可以在配置文件或头文件中定义一个常量,该常量定义应用程序根目录的绝对路径。例如:

define('APPROOT', __DIR__);
或:

然后,您可以在应用程序中定义路径,而无需依赖脚本的所在位置,例如:

$log_file = APPROOT . '/log/foo.log';

不可以。除了与当前正在执行的文件相关的元数据外,PHP不提供任何其他元数据

要使文件路径更容易处理,可以在配置文件或头文件中定义一个常量,该常量定义应用程序根目录的绝对路径。例如:

define('APPROOT', __DIR__);
或:

然后,您可以在应用程序中定义路径,而无需依赖脚本的所在位置,例如:

$log_file = APPROOT . '/log/foo.log';

谢谢你的回答。我还有另一个问题:有没有一种方法可以生成一个PHP全局变量或常量?在配置文件中的某个地方,比如php.ini,我不知道。你为什么想要这个?我需要声明常量,它可以在任何地方使用,不包括任何内容。谢谢你的回答。我还有另一个问题:有没有一种方法可以生成一个PHP全局变量或常量?在配置文件中的某个地方,比如php.ini,我不知道。你为什么要这么做?我需要声明常量,它可以在任何地方使用,但不包括任何内容。