【发布时间】:2020-04-06 11:26:32
【问题描述】:
如何防止同一个文件被包含两次?这是我的源代码:
<?php
error_reporting(-1);
ini_set('display_errors', 'On');
ini_set('html_errors', 0);
if (!file_exists('ccc.php'))
link('bbb.php', 'ccc.php');
if (!file_exists('ddd.php'))
link('bbb.php', 'ddd.php');
require_once realpath('ccc.php');
require_once realpath('ddd.php');
$bbb = new Bbb();
echo $bbb->bb();
我收到:
Fatal error: Cannot declare class Bbb, because the name is already in use in /path/to/ddd.php on line 2
它不适用于realpath,因为它只返回链接的路径,而不是目标。我试过readlink,但不幸的是这只适用于符号链接。
【问题讨论】:
-
你可以用 ls -i 获取inode,看看它是否已经被你使用了?
-
@BugFinder 这不会导致性能问题吗?当然,必须有一种方法可以在不使用
exec的情况下获取php中的链接目标。 -
好吧php.net/manual/en/function.fileinode.php 似乎也这样做了......
-
我正在尝试找到一种方法来反转
fileinode,以便我收到链接的目标但没有运气。 -
我不认为你可以。硬链接隐藏可见性,我记得软链接有一个计数器..
标签: php hyperlink realpath readlink