php fopen中的路径怎么写?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15450947/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 09:15:41  来源:igfitidea点击:

How to write the path in php fopen?

phppath

提问by user1552480

I have some problems with the relative and absolute paths in php fopen. I have the following directories:

我在 php fopen 中的相对和绝对路径有一些问题。我有以下目录:

project:
    scripts:
        myscript.php
    logs:
        mylog.log

I want to open mylog.logfrom myscript.phpand I don't know how to specify the path. I tried

我想开mylog.logmyscript.php,我不知道如何指定的路径。我试过

fopen('../logs/mylog.log', "a")

but it won't work. Thanks.

但它不会工作。谢谢。

LE: Thanks for you answers.

LE:谢谢你的回答。

回答by Jeremy Blalock

In php, there are a couple of global constants that could be of help to you. Namely, __DIR__gives you the directory of the current file ('.'just gives you the directory of the root script executing).

在 php 中,有几个全局常量可能对您有所帮助。即,__DIR__为您提供当前文件的目录('.'只为您提供执行根脚本的目录)。

So what you want is:

所以你想要的是:

fopen(__DIR__ . '/../logs/mylog.log', "a")

回答by Brandon Wamboldt

This should work:

这应该有效:

fopen(__DIR__ . '/../logs/mylog.log', "a");

or in PHP < 5.3:

或在 PHP < 5.3 中:

fopen(dirname(__FILE__) . '/../logs/mylog.log', "a");

回答by Can Geli?

You can use $_SERVER['DOCUMENT_ROOT']which gives the document root of the virtual host.

您可以使用$_SERVER['DOCUMENT_ROOT']which 给出虚拟主机的文档根目录。

eg: $_SERVER['DOCUMENT_ROOT']."/log/mylog.log"

例如: $_SERVER['DOCUMENT_ROOT']."/log/mylog.log"