php file_put_contents 路径问题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6399349/
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-26 00:14:59  来源:igfitidea点击:

file_put_contents path question

phpfilepath

提问by fish man

I want to create html page using php code form c:\www\test\script\new\creat_html.phpto c:\www\test\html\index.html.

我想使用 php 代码形式创建 html 页面c:\www\test\script\new\creat_html.phpc:\www\test\html\index.html.

Now in c:\www\test\script\new\creat_html.phphow do I set the path?

现在c:\www\test\script\new\creat_html.php如何设置路径?

I use dirname(__FILE__), but it just gets the parent path. How to? Thanks.

我使用dirname(__FILE__),但它只是获取父路径。如何?谢谢。

file_put_contents( dirname(__FILE__) . '/index.html', $html);

采纳答案by OZ_

dirname(__FILE__)will return:
c:\www\test\script\new\
you need c:\www\test\html\index.html
so you need to up to 2 levels, you can do it with ..symbol in path:
c:\www\test\script\new\..\..\= c:\www\test\
now you can add necessary part of path:
dirname(__FILE__).'../../html/index.html'

dirname(__FILE__)将返回:
c:\www\test\script\new\
您需要, c:\www\test\html\index.html
因此您需要最多 2 个级别,您可以使用..路径中的符号来完成:
c:\www\test\script\new\..\..\=c:\www\test\
现在您可以添加路径的必要部分:
dirname(__FILE__).'../../html/index.html'

回答by genesis

file_put_contents("../../index.html", $html);

easy - simple

简单——简单

EDIT: You have to access creat_html.php directly, or this solution won't work !

编辑:您必须直接访问 creat_html.php,否则此解决方案将不起作用!