__FILE__ 是什么意思?

问题描述 投票:0回答:4

我有来自 Codeigniter 的以下代码

index.php

我的理解是,

如果

/
(本例中为 CIcore_1_7_1)中字符串位置的
$system_folder
false
, 如果
realpath
函数存在并且 (?) 不是
false
$system_folder
被分配给 (?)
/$system_folder
。 否则
$system_folder
被分配给
$system_folder
,并将
\\
替换为
/

Q1。 realpath函数是什么意思?

Q2。这是什么意思?

@realpath(dirname(__FILE__))

Q3。我对吗?我是不是有什么误解?

Q4。您需要以下哪种情况?

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); 
}
php codeigniter path constants
4个回答
63
投票
  1. realpath()
    函数为您提供文件系统路径,并解析任何符号链接和目录遍历(例如../../)。
    dirname()
    函数只提供目录,而不提供其中的文件。

  2. __FILE__
    是一个神奇的常量,它为您提供当前 .php 文件的文件系统路径(
    __FILE__
    所在的文件,而不是包含它的文件)。

  3. 听起来不错。

  4. 这是从 Windows 风格 (\) 路径转换为 Unix 风格 (/)。


14
投票

__FILE__
只是当前文件的名称。
realpath(dirname(__FILE__))
获取文件所在目录的名称——本质上是应用程序安装的目录。
@
是 PHP 抑制错误的极其愚蠢的方法。


7
投票
__FILE__

完整路径和文件名 文件。如果在包含内部使用,则 返回包含的文件的名称。 从 PHP 4.0.2 开始,FILE 始终 包含绝对路径 符号链接已解决,而在旧版本中 它包含相对路径的版本 在某些情况下。


string dirname  ( string $path  )

给定一个包含文件路径的字符串,该函数将返回 目录的名称。


str_replace("\\", "/", $system_folder)

您需要使不同操作系统之间的路径分隔符保持一致。 Windows 使用 \ 而 *nix 使用 /,您可以保留 /。


0
投票

此回声可能有助于澄清差异:

echo 基本名称(目录名(FILE)); // 简单 ajax 聊天

echo $_SERVER["PHP_SELF"]; // /wp-admin/plugins.php

echo $_SERVER["DOCUMENT_ROOT"]; // /var/www/path/example.com/httpdocs

echo 目录名(FILE); // /var/www/path/example.com/httpdocs/wp-content/plugins/simple-ajax-chat

echo getcwd(); // /var/www/path/example.com/httpdocs/wp-admin

回显文件; // /var/www/path/example.com/httpdocs/wp-content/plugins/simple-ajax-chat/simple-ajax-chat.php

© www.soinside.com 2019 - 2024. All rights reserved.