使用scandir()在目录(PHP)中查找文件夹 [英] Using scandir() to find folders in a directory (PHP)

查看:199
本文介绍了使用scandir()在目录(PHP)中查找文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用这段代码:

$target = 'extracted/' . $name[0];  
$scan = scandir($target);

扫描用于zip上传的文件夹的目录。我想要找到我的 $ target 文件夹中的所有文件夹,所以我可以删除它们及其内容,只留下 $中的文件目标目录。

To scan the directory of a folder which is used for zip uploads. I want to be able to find all the folders inside my $target folder so I can delete them and their contents, leaving only the files in the $target directory.

一旦我已经返回文件夹的内容,我不知道如何区分文件夹和文件,以便能够删除文件夹。

Once I have returned the contents of the folder, I don't know how to differentiate between the folders and the files to be able to delete the folders.

此外,我被告知, rmdir()功能无法删除其中包含内容的文件夹有没有办法呢?

Also, I have been told that the rmdir() function can't delete folders which have content inside them, is there any way around this?

谢谢,Ben。

推荐答案

p>要确定是否有文件夹或文件使用函数 is_dir() is_file()

To determine whether or not you have a folder or file use the functions is_dir() and is_file()

例如:


$path = 'extracted/' . $name[0];
$results = scandir($path);

foreach ($results as $result) {
    if ($result === '.' or $result === '..') continue;

    if (is_dir($path . '/' . $result)) {
        //code to use if directory
    }
}

这篇关于使用scandir()在目录(PHP)中查找文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆