PHP 根文件夹

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

PHP root folder

phpfileroot

提问by James

rename('/images/old_name.jpg', '/images/new_name.jpg');

This code gives file not found.

这段代码给出了file not found

Script, where files are called is placed inside /source/folder.

调用文件的脚本放置在/source/文件夹中。

Files can be opened from http://site.com/images/old_name.jpg

文件可以从 http://site.com/images/old_name.jpg

How to get these files from root?

如何从root获取这些文件?

回答by Gumbo

renameis a filesystem function and requires filesystem paths. But it seems that you're using URI paths.

rename是一个文件系统函数,需要文件系统路径。但似乎您正在使用 URI 路径。

You can use $_SERVER['DOCUMENT_ROOT']to prepend the path to the document root:

您可以使用$_SERVER['DOCUMENT_ROOT']预先添加到文档根目录的路径:

rename($_SERVER['DOCUMENT_ROOT'].'/images/old_name.jpg', $_SERVER['DOCUMENT_ROOT'].'/images/new_name.jpg');

Or for more flexibility, use dirnameon the path to the current file __FILE__:

或者为了更灵活,dirname在当前文件的路径上使用__FILE__

rename(dirname(__FILE__).'/images/old_name.jpg', dirname(__FILE__).'/images/new_name.jpg');

Or use relative paths. As you're in the /scriptfolder, ..walks one directory level up:

或者使用相对路径。当您在/script文件夹中时,..向上走一级目录:

rename('../images/old_name.jpg', '../images/new_name.jpg');

回答by adamse

In PHP the root (/) is the root of the filesystem not the "webroot". If the php-file is in the /source/directory and images are in /source/images/then this will work:

在 PHP 中,根 ( /) 是文件系统的根,而不是“webroot”。如果 php 文件在/source/目录中并且图像在其中,/source/images/那么这将起作用:

rename('images/old_name.jpg', 'images/new_name.jpg');