使用 PHP 删除 __MACOSX 文件夹?

发布于 2024-08-03 03:51:39 字数 542 浏览 2 评论 0原文

有人有过用 PHP 删除 __MACOSX 文件夹的经验吗?

该文件夹是我解压存档后生成的,但我似乎无法删除它。

is_dir 函数在文件上返回 false,使递归删除脚本失败(因为存档内部是“临时”文件),因此目录不为空。

我在 PHP5 中使用内置的 ZipArchive 类(extractTo 方法)。

我使用的 rmdir 脚本是我在 php.net 上找到的:

<?php
// ensure $dir ends with a slash
function delTree($dir) {
    $files = glob( $dir . '*', GLOB_MARK );
    foreach( $files as $file ){
        if( substr( $file, -1 ) == '/' )
            delTree( $file );
        else
            unlink( $file );
    }
    rmdir( $dir );
}
?> 

Has anyone had any experience with deleting the __MACOSX folder with PHP?

The folder was generated after I unzipped an archive, but I can't seem to do delete it.

The is_dir function returns false on the file, making the recursive delete scripts fail (because inside the archive is the 'temp' files) so the directory isn't empty.

I'm using the built-in ZipArchive class (extractTo method) in PHP5.

The rmdir script I'm using is one I found on php.net:

<?php
// ensure $dir ends with a slash
function delTree($dir) {
    $files = glob( $dir . '*', GLOB_MARK );
    foreach( $files as $file ){
        if( substr( $file, -1 ) == '/' )
            delTree( $file );
        else
            unlink( $file );
    }
    rmdir( $dir );
}
?> 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

沫离伤花 2024-08-10 03:51:39

我从 http://www.php.net/rmdir 找到了该函数的改进版本,该版本需要PHP5。

  • 此函数使用 DIRECTORY_SEPARATOR 而不是 /。 PHP 将 DIRECTORY_SEPARATOR 定义为运行操作系统的正确字符(“/”或“\”)。
  • 目录位置不需要以斜杠结尾。
  • 该函数在完成时返回 truefalse
函数deleteDirectory($dir) {
    if (!file_exists($dir)) 返回 true;
    if (!is_dir($dir)) 返回 unlink($dir);
    foreach (scandir($dir) as $item) {
        if ($item == '.' || $item == '..') 继续;
        if (!deleteDirectory($dir.DIRECTORY_SEPARATOR.$item)) 返回 false;
    }
    返回 rmdir($dir);
}

I found an improved version of the function from http://www.php.net/rmdir that requires PHP5.

  • This function uses DIRECTORY_SEPARATOR instead of /. PHP defines DIRECTORY_SEPARATOR as the proper character for the running OS ('/' or '\').
  • The Directory Location doesn't need to end with a slash.
  • The function returns true or false on completion.
function deleteDirectory($dir) {
    if (!file_exists($dir)) return true;
    if (!is_dir($dir)) return unlink($dir);
    foreach (scandir($dir) as $item) {
        if ($item == '.' || $item == '..') continue;
        if (!deleteDirectory($dir.DIRECTORY_SEPARATOR.$item)) return false;
    }
    return rmdir($dir);
}
故事灯 2024-08-10 03:51:39

您使用的是哪个操作系统和版本?


您需要更正目录和文件的路径。

// ensure $dir ends with a slash
function delTree($dir) {

    foreach( $files as $file ){
        if( substr( $file, -1 ) == '/' )
            delTree( $dir.$file );
        else
            unlink( $dir.$file );
    }
    rmdir( $dir );
}

Which OS and version are you using?


You need to correct the paths to the directory and files.

// ensure $dir ends with a slash
function delTree($dir) {

    foreach( $files as $file ){
        if( substr( $file, -1 ) == '/' )
            delTree( $dir.$file );
        else
            unlink( $dir.$file );
    }
    rmdir( $dir );
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文