php __FILE__ 是什么意思?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1506459/
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
What does __FILE__ mean?
提问by shin
I have the following code from Codeigniter index.php
我有来自 Codeigniter 的以下代码 index.php
My understanding is that,
我的理解是,
If /of string position in $system_folder(in this case CIcore_1_7_1) is false,
and if realpathfunction exists AND (?) is not false,
$system_folderis assigned to (?) /$system_folder.
else $system_folderis assigned to $system_folderwith replacing \\with /.
如果(在这种情况下 CIcore_1_7_1)中/的字符串位置$system_folder是false,并且如果realpath函数存在并且 (?) 不是false,
$system_folder则分配给 (?) /$system_folder。else$system_folder被分配给$system_folder替换\\为/。
Q1. What does realpath function means?
一季度。realpath 函数是什么意思?
Q2. What does this mean?
Q2。这是什么意思?
@realpath(dirname(__FILE__))
Q3. Am I right? Do I have any misunderstanding?
Q3。我对吗?我有什么误解吗?
Q4. What kind of situation do you need the following?
第 4 季度。您需要以下哪种情况?
str_replace("\", "/", $system_folder)
$system_folder = "CIcore_1_7_1";
/*
|---------------------------------------------------------------
| SET THE SERVER PATH
|---------------------------------------------------------------
|
| Let's attempt to determine the full-server path to the "system"
| folder in order to reduce the possibility of path problems.
| Note: We only attempt this if the user hasn't specified a
| full server path.
|
*/
if (strpos($system_folder, '/') === FALSE)
{
if (function_exists('realpath') AND @realpath(dirname(__FILE__)) !== FALSE)
{
$system_folder = realpath(dirname(__FILE__)).'/'.$system_folder;
}
}
else
{
// Swap directory separators to Unix style for consistency
$system_folder = str_replace("\", "/", $system_folder);
}
回答by Greg
The
realpath()function gives you the file-system path, with any symbolic links and directory traversing (e.g. ../../) resolved. Thedirname()function gives you just the directory, not the file within it.__FILE__is a magic constant that gives you the filesystem path to the current .php file (the one that__FILE__is in, not the one it's included by if it's an include.Sounds about right.
This is to translate from Windows style (\) paths to Unix style (/).
该
realpath()函数为您提供文件系统路径,并解析任何符号链接和目录遍历(例如 ../../)。该dirname()函数只为您提供目录,而不是其中的文件。__FILE__是一个神奇的常量,它为您提供当前 .php 文件的文件系统路径(所在的文件__FILE__,而不是包含的文件,如果它是包含的。听起来是对的。
这是从 Windows 样式 (\) 路径转换为 Unix 样式 (/)。
回答by hobbs
__FILE__is simply the name of the current file. realpath(dirname(__FILE__))gets the name of the directory that the file is in -- in essence, the directory that the app is installed in. And @is PHP's extremely silly way of suppressing errors.
__FILE__只是当前文件的名称。realpath(dirname(__FILE__))获取文件所在目录的名称——本质上是安装应用程序的目录。这@是 PHP 抑制错误的极其愚蠢的方式。
回答by eKek0
__FILE__
The full path and filename of the file. If used inside an include, the name of the included file is returned. Since PHP 4.0.2, FILEalways contains an absolute path with symlinks resolved whereas in older versions it contained relative path under some circumstances.
文件的完整路径和文件名。如果在包含中使用,则返回包含文件的名称。自 PHP 4.0.2 起,FILE始终包含已解析符号链接的绝对路径,而在旧版本中,它在某些情况下包含相对路径。
string dirname ( string $path )Given a string containing a path to a file, this function will return the name of the directory.
string dirname ( string $path )给定一个包含文件路径的字符串,此函数将返回目录的名称。
str_replace("\", "/", $system_folder)
You need this to be consisten in path separators between different operating systems. Windows uses \ and *nix uses /, you keep with /.
您需要在不同操作系统之间的路径分隔符中保持一致。Windows 使用 \ 而 *nix 使用 /,您使用 /。

