简体   繁体   English

使用硬链接时如何防止同一文件被包含两次?

[英]How can I prevent the same file being included twice when using hardlinks?

How can I prevent the same file being included twice?如何防止同一文件被包含两次? Here is my source code:这是我的源代码:

<?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();

I receive:我收到:

Fatal error: Cannot declare class Bbb, because the name is already in use in /path/to/ddd.php on line 2

It's not working with realpath because it just returns the path of the link, not the target.它不适用于realpath因为它只返回链接的路径,而不是目标。 I've tried with readlink but unfortunately that only works for symlinks.我试过readlink但不幸的是它只适用于符号链接。

I have a working solution.我有一个可行的解决方案。 It's not ideal but for a quick fix it will do, until I am able to make sure the same files are never included twice:这并不理想,但它可以快速修复,直到我能够确保永远不会包含相同的文件两次:

<?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');

function includeFile($fileName)
{
    foreach (get_included_files() as $file)
    {
        if (fileinode($file) === fileinode($fileName))
            return null;
    }
    require_once $fileName;
}
includeFile('ccc.php');
includeFile('ddd.php');

$bbb = new Bbb();
echo $bbb->bb();

Thanks to BugFinder for suggesting I use fileinode .感谢BugFinder建议我使用fileinode

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何防止在包含的文件中两次包含相同的文件? - How can I prevent to include same file twice in an included file? 如何防止有人使用促销代码两次? - How can I prevent someone using a promo code twice? 如何防止用户在 WordPress 上两次上传同一个文件 - How to prevent users from uploading the same file twice WordPress 当代码甚至没有达到执行点时,如何将我的类包含两次? - How is my class being included twice when the code doesn't even reach that point of execution? 如何防止使用php在mysql中插入两次相同的记录 - how to prevent same record to be inserted twice in mysql using php 如何使用url限制正在查看或下载的文件? 我希望使用PHP脚本下载相同的文件 - how can i restrict a file being viewed or downloaded using url? i want same file to be downloaded using php script 当我刷新网页时,评论被保存在一个文件上两次 - comments are being saved twice on a file when i refresh the webpage 如何两次插入同一张表? - How can I INSERT into same table twice? PHP防止两次包含相同的文件 - PHP prevent including same file twice 在 php 中使用基于文件的计数器时如何防止竞争条件? - How can I prevent a race condition when using a file based counter in php?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM