php目录读取并删除现有文件

发布于 2024-12-22 00:41:00 字数 709 浏览 0 评论 0原文

我试图确保当用户上传个人资料图片时,目录“profile_pic”中只能有一张图像。我正在使用 while 循环打开并读取目录,但 @unlink 不起作用。下面是php代码:

        $directory = "uploads\\".$id."\images\profile_pic\\";
        if (glob($directory . "*.jpg") != false) {
            $filecount = count(glob($directory . "*.jpg"));
            if ($filecount > 0) {
                //delete exiting pics in folder profile_pic
                $handle = opendir($directory);
                while ($handle && ($file = readdir($handle)) !== false) {
                    if ( unlink($entry)) {
                    $msg .= "File Deleted";
                    }
                }
                closedir($directory);
            }
        }

I am trying to make sure that when the user uploads a profile picture, there can only be one image within the dir "profile_pic". I am openning and reading the dir with a while loop but the @unlink is not working. Below is the php code:

        $directory = "uploads\\".$id."\images\profile_pic\\";
        if (glob($directory . "*.jpg") != false) {
            $filecount = count(glob($directory . "*.jpg"));
            if ($filecount > 0) {
                //delete exiting pics in folder profile_pic
                $handle = opendir($directory);
                while ($handle && ($file = readdir($handle)) !== false) {
                    if ( unlink($entry)) {
                    $msg .= "File Deleted";
                    }
                }
                closedir($directory);
            }
        }

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

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

发布评论

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

评论(1

香草可樂 2024-12-29 00:41:00

抱歉,您在哪里定义了 $entry - 您是否曾为其分配过值?为什么要对 glob() 进行两次完全相同的调用,而不是在变量中捕获第一次调用的结果?当您已经通过 glob() 编辑并获得了文件列表时,为什么还要费力地使用 opendir() 来列出文件呢?特别是当您不对要删除的文件应用相同的 *.jpg 过滤器时...

只需执行以下操作:

// Forward slashes work on Windows too (in PHP, at least)
$directory = "uploads/".$id."/images/profile_pic";

if (($existing = glob($directory . "/*.jpg")) !== false) { // Get a list of files...
  foreach ($existing as $file) { // ...loop them...
    $msg .= (unlink($directory."/".$file)) ? "File $file deleted\n" : "Could not delete $file\n"; // ...and delete them
  }
} else $msg .= "Listing directory failed\n";

Sorry, where did you define $entry - did you ever assign a value to it? And why do you make exactly the same call to glob() twice instead of catching the result of the first call in a variable? And why bother to opendir() to list files when you have already glob()ed and obtained that list of files? Especially when you don't apply the same *.jpg filter to the files you are deleting...

Just do this:

// Forward slashes work on Windows too (in PHP, at least)
$directory = "uploads/".$id."/images/profile_pic";

if (($existing = glob($directory . "/*.jpg")) !== false) { // Get a list of files...
  foreach ($existing as $file) { // ...loop them...
    $msg .= (unlink($directory."/".$file)) ? "File $file deleted\n" : "Could not delete $file\n"; // ...and delete them
  }
} else $msg .= "Listing directory failed\n";
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文