Using scandir() to find folders in a directory (PHP)(使用 scandir() 在目录中查找文件夹 (PHP))
问题描述
我正在使用这段代码:
$target = 'extracted/' .$名称[0];$scan = scandir($target);扫描用于 zip 上传的文件夹的目录.我希望能够在我的
$target文件夹中找到所有文件夹,以便我可以删除它们及其内容,只留下$target目录中的文件.一旦我返回了文件夹的内容,我不知道如何区分文件夹和文件才能删除文件夹.
另外,我被告知
rmdir()函数无法删除其中包含内容的文件夹,有什么办法可以解决这个问题吗?谢谢,本.
解决方案使用
is_dir()和is_file()函数确定你是否有文件夹或文件代码>例如:
<前>$path = '提取/' .$名称[0];$results = scandir($path);foreach ($results as $result) {if ($result === '.' or $result === '..') continue;if (is_dir($path . '/' . $result)) {//如果目录使用的代码}}
I am using this peice of code:
$target = 'extracted/' . $name[0];
$scan = scandir($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.
Also, I have been told that the rmdir() function can't delete folders which have content inside them, is there any way around this?
Thanks, Ben.
To determine whether or not you have a folder or file use the functions is_dir() and is_file()
For example:
$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)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:使用 scandir() 在目录中查找文件夹 (PHP)
基础教程推荐
- PHP 类:全局变量作为类中的属性 2021-01-01
- 如何在 Laravel 中使用 React Router? 2022-01-01
- 如何在 Laravel 5.3 注册中添加动态下拉列表列? 2021-01-01
- 如何替换eregi() 2022-01-01
- PHP PDO MySQL 查询 LIKE ->多个关键词 2021-01-01
- 有什么方法可以用编码 UTF-8 而不是 Unicode 返回 PHP`json_encode`? 2021-01-01
- 学说 dbal querybuilder 作为准备好的语句 2022-01-01
- Cron Jobs 调用带有变量的 PHP 脚本 2022-01-01
- YouTube API v3 点赞视频,但计数器不增加 2022-01-01
- 在PHP中根据W3C规范Unicode 2022-01-01
