PHP unlink命令删除多个文件

发布于 2024-12-29 20:19:10 字数 739 浏览 4 评论 0原文

我正在包含以下文件的目录 (user-images/1/p/) 上调用 php unlink() 函数:

1.jpg
1-s.jpg
1big.jpg
2.jpg
2-s.jpg
2big.jpg

实际调用本身如下

unlink('user-images/1/p/1big.jpg');

:仅删除 1big.jpg 时,它会删除其中包含 1 的所有文件(1big.jpg1-s.jpg1.jpg)。我对此进行了相当多的研究,似乎找不到任何人发布类似的问题。

编辑: 下面是完整的脚本,实际上没有太多内容,看不出有什么会受到影响。我以前也从未见过这个:(

<?PHP
unlink('user-images/1/p/1.jpg');
unlink('user-images/1/p/1-s.jpg');
$uid = '1';
$fileName = '467';
$image = '/friskyfriends/user-images/1/p/1-big.jpg';
$width = 320;
$height = 320;
buildPics();
//buildPics($uid,$fileName,$image,$width,$height);
?>

I am calling the php unlink() function on a directory (user-images/1/p/) containing the following files:

1.jpg
1-s.jpg
1big.jpg
2.jpg
2-s.jpg
2big.jpg

The actual call itself is as follows:

unlink('user-images/1/p/1big.jpg');

Instead of just deleting 1big.jpg, it deletes all files with a 1 in them (1big.jpg, 1-s.jpg, 1.jpg). I've researched this quite a bit and can't seem to find anyone posting with a similar issue.

EDIT:
below is the full script, not much there really, don't see how anything could be affected. I've never seen this before either :(

<?PHP
unlink('user-images/1/p/1.jpg');
unlink('user-images/1/p/1-s.jpg');
$uid = '1';
$fileName = '467';
$image = '/friskyfriends/user-images/1/p/1-big.jpg';
$width = 320;
$height = 320;
buildPics();
//buildPics($uid,$fileName,$image,$width,$height);
?>

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

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

发布评论

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

评论(4

默嘫て 2025-01-05 20:19:11

我认为您正在寻找 GLOB 函数,它允许使用通配符进行删除。

例子

foreach (glob("*.jpg") as $filename) {
   echo "$filename size " . filesize($filename) . "\n";
   unlink($filename);
}

I think you are looking for the GLOB function, which allows delete with wildcards.

example

foreach (glob("*.jpg") as $filename) {
   echo "$filename size " . filesize($filename) . "\n";
   unlink($filename);
}
爱情眠于流年 2025-01-05 20:19:11

当运行仅包含 unlink 代码的 php 文件时,它工作得很好。我追踪了其余的包含内容(花了很长时间 :( )并发现了之前编码中的一些问题。仍然不确定为什么会影响静态 unlink() 调用。也就是说,这是其他地方的问题在已解决的代码中,我感谢大家为帮助我解决此问题而付出的时间和精力......

When running the php file with only the unlink code in it, it worked fine. I traced through the rest of my includes (took forever :( ) and found some issues in coding before. Still not sure why that affected a static unlink() call. That said, it was an issue elsewhere in the code which has been resolved. I appreciate the time and effort everyone put forth to help me troubleshoot this issue...

久随 2025-01-05 20:19:11

另一种方法和更明确的解决方案可能是路径

$search_text = "logfiles";
foreach(glob("/path/to/your/directory/$search_text*") as $filename)
{
    if(file_exists($filename)
    {
        echo "$filename size " . filesize($filename) . "\n";
        unlink($filename);
    }
    else
    {
         //no need to write because file already found with glob function
    }
}

another approach and more definitive solution could be with a path

$search_text = "logfiles";
foreach(glob("/path/to/your/directory/$search_text*") as $filename)
{
    if(file_exists($filename)
    {
        echo "$filename size " . filesize($filename) . "\n";
        unlink($filename);
    }
    else
    {
         //no need to write because file already found with glob function
    }
}
涙—继续流 2025-01-05 20:19:11

请为您要删除的图像添加取消链接功能。

unlink($image);

Please add the unlink function for which image you want to delete.

unlink($image);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文