php 从文件夹中删除图像

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5535202/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 21:44:36  来源:igfitidea点击:

Delete images from a folder

phpdelete-file

提问by arcus_marcus

I want to to destroy all images within a folder with PHP how can I do this?

我想用 PHP 销毁文件夹中的所有图像,我该怎么做?

回答by Christian

foreach(glob('/www/images/*.*') as $file)
    if(is_file($file))
        @unlink($file);

glob()returns a list of file matching a wildcard pattern.

glob()返回匹配通配符模式的文件列表。

unlink()deletes the given file name (and returns if it was successful or not).

unlink()删除给定的文件名(如果成功与否则返回)。

The @before PHP function names forces PHP to suppress function errors.

@之前的PHP函数名部队PHP来抑制功能的错误。

The wildcard depends on what you want to delete. *.*is for all files, while *.jpgis for jpg files. Note that globalso returns directories, so If you have a directory named images.jpg, it will return it as well, thus causing unlinkto fail since it deletes files only.

通配符取决于您要删除的内容。*.*适用于所有文件,而*.jpg适用于 jpg 文件。请注意,glob它还返回目录,因此如果您有一个名为 的目录images.jpg,它也会返回它,从而导致unlink失败,因为它仅删除文件。

is_file()ensures you only attempt to delete files.

is_file()确保您只尝试删除文件。

回答by ThiefMaster

The easiest (non-recursive) way is using glob():

最简单(非递归)的方法是使用glob()

$files = glob('folder/*.jpg');
foreach($files as $file) {
    unlink($file);
}

回答by Dejan Marjanovic

$images = glob("images/*.jpg");
foreach($images as $image){
     @unlink($image);
}

回答by Bhanu Prakash Pandey

use unlink and glob function

unlink and glob function

for more see this link http://php.net/manual/en/function.unlink.phpand http://php.net/manual/en/function.glob.php

有关更多信息,请参阅此链接 http://php.net/manual/en/function.unlink.phphttp://php.net/manual/en/function.glob.php