简体   繁体   English

识别PHP中的根目录

[英]Recognizing the root dir in PHP

I'm writing a PHP function that will delete all the files in the directory that's passed to it (and eventually, possibly expand it to a recursive delete). 我正在编写一个PHP函数,它将删除传递给它的目录中的所有文件(并最终将其扩展为递归删除)。 To be safe, I want to make sure that, through a bug or something, I don't try to delete anything if the directory passed in is the root directory. 为了安全起见,我想确保通过错误或其他原因,如果传入的目录是根目录,我不会尝试删除任何内容。

File permissions should protect me to a large extent, but just in case, and especially if I expand it to a recursive delete I just want to take that extra step. 文件权限应在很大程度上保护我,但以防万一,尤其是如果我将其扩展为递归删除时,我只想采取这一额外步骤。

As a complicating factor, this code may be run in a windows machine or a linux machine, so the root directory may look like 'C:\\' or '/'. 作为复杂因素,此代码可能在Windows机器或linux机器上运行,因此根目录可能看起来像“ C:\\”或“ /”。 I assume there are other ways that really refer to the root as well, possibly 'c:\\temp..' 我假设还有其他真正引用根的方法,可能是'c:\\ temp ..'。

So, is there a reliable way in PHP to recognize that a dir spec resolves to the root of the file system? 那么,PHP是否有可靠的方法来识别目录规范解析为文件系统的根目录?

Elaboration... 阐述...

I'm writing PHPUnit tests for a web app and I'm trying to create a framework where the state of the app is backed up before the tests are run and restored afterwards. 我正在为一个Web应用程序编写PHPUnit测试,并且试图创建一个框架,在该框架中可以在运行测试之前备份应用程序的状态,然后再进行测试还原。 The app allows users to upload files. 该应用程序允许用户上传文件。 Depending on what the file is it is copied to one of several possible directories. 根据文件的不同,将其复制到几个可能的目录之一。

To save and restore the state of the app those directories need to be copied somewhere, the tests run, then the directories need to have their files deleted and retreived from the backup. 要保存和恢复应用程序的状态,需要将这些目录复制到某个位置,然后运行测试,然后删除目录并从备份中获取其文件。

The location of these directories can vary from one machine to another and I know that some people put them outside of the web app. 这些目录的位置在一台机器上可能会有所不同,我知道有些人将它们放在Web应用程序之外。 There is a configuration file that can be read by the test that gives the location of those directories for the given machine. 测试可以读取一个配置文件,该文件提供给定计算机的那些目录的位置。

If I don't restrict all these directories to a specific dir tree it's difficult to do the jailing. 如果我不将所有这些目录都限制为特定的目录树,则很难进行管理。 If I do restrict these directories to a specific dir tree then some people will have to reconfigure their machines. 如果我确实将这些目录限制为特定的目录树,那么某些人将不得不重新配置其计算机。

You should have a defined root folder, which you never go above, aka jailing. 您应该有一个已定义的根文件夹,也永远不要放在上面。 The root folder is not the only folder where severe damage can be done. 根文件夹不是唯一可以造成严重损坏的文件夹。

Edit. 编辑。

Although I still advocate using some sort of jailing, I suppose you could recognize the root folder by stripping out any drive-letters and translating \\ to /. 尽管我仍然主张使用某种类型的jail,但我想您可以通过剥离所有驱动器字母并将\\转换为/来识别根文件夹。 The root folder would then always be a single /. 这样,根文件夹将始终为单个/。

function isRootFolder($dirpath) {
    list($drive, $path) = explode(':', str_replace('\\', '/', $dirpath), 2);

    return $path == '/';
}

Try, this function: 试试看,这个功能:

function is_root_dir($path)
{
    $clean_path = realpath($path);
    if($clean_path == '/' || preg_match('/[a-z]:\\/i', $clean_path))
    {
        return true;
    }
    else
    {
        return false;
    }
}

It's not tested, I just wrote it in the editor here. 它未经测试,我只是在这里的编辑器中编写的。 realpath() resolves the path, folowing simbolic links and resolving stuff like: c:\\temp.. == c:\\ realpath()解析路径,遵循符号链接并解决诸如:c:\\ temp .. == c:\\

Edit: In the end you should folow the advice that nikc gave you, define a list of directories that are safe to delete. 编辑:最后,您应该遵循nikc给您的建议,定义一个可以安全删除的目录列表。

I use this: 我用这个:

if (dirname($target)==$target) { // you're at the root dir

(is portable between Microsoft and everything else) (可在Microsoft和其他所有产品之间移植)

C. C。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM