__FILE__ 是什么意思?

发布于 2024-08-06 22:12:12 字数 1403 浏览 3 评论 0原文

我有来自 Codeigniter index.php 的以下代码,

我的理解是,

如果 / 位于 $system_folder 中的字符串位置(在本例中为 CIcore_1_7_1)是, 如果 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); 
}

I have the following code from 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 realpath function exists AND (?) is not false,
$system_folder is assigned to (?) /$system_folder.
else $system_folder is assigned to $system_folder with replacing \\ with /.

Q1. What does realpath function means?

Q2. What does this mean?

@realpath(dirname(__FILE__))

Q3. Am I right? Do I have any misunderstanding?

Q4. What kind of situation do you need the following?

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); 
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

小霸王臭丫头 2024-08-13 22:12:12
  1. realpath() 函数为您提供文件系统路径,其中任何符号链接和目录遍历(例如../../)都已解决。 dirname() 函数只提供目录,而不提供其中的文件。

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

  3. 听起来不错。

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

  1. The realpath() function gives you the file-system path, with any symbolic links and directory traversing (e.g. ../../) resolved. The dirname() function gives you just the directory, not the file within it.

  2. __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.

  3. Sounds about right.

  4. This is to translate from Windows style (\) paths to Unix style (/).

擦肩而过的背影 2024-08-13 22:12:12

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

__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.

沙沙粒小 2024-08-13 22:12:12
__FILE__

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


字符串目录名(字符串$path)

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


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

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

__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, FILE always
contains an absolute path with
symlinks resolved whereas in older
versions it contained relative path
under some circumstances.


string dirname  ( string $path  )

Given a string containing a path to a file, this function will return
the name of the directory.


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 /.

凉风有信 2024-08-13 22:12:12

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

//  simple-ajax-chat

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

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

echo dirname(__FILE__);
//  /var/www/path/example.com/httpdocs/wp-content/plugins/simple-ajax-chat

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

echo __FILE__;
// /var/www/path/example.com/httpdocs/wp-content/plugins/simple-ajax-chat/simple-ajax-chat.php```

This echo might help clarify the difference:

//  simple-ajax-chat

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

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

echo dirname(__FILE__);
//  /var/www/path/example.com/httpdocs/wp-content/plugins/simple-ajax-chat

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

echo __FILE__;
// /var/www/path/example.com/httpdocs/wp-content/plugins/simple-ajax-chat/simple-ajax-chat.php```
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文