从文件夹中删除图像 [英] Delete images from a folder
问题描述
我想用PHP销毁文件夹中的所有图像,我该怎么做?
I want to to destroy all images within a folder with PHP how can I do this?
推荐答案
foreach(glob('/www/images/*.*') as $file)
if(is_file($file))
@unlink($file);
glob()
返回与通配符模式匹配的文件列表.
glob()
returns a list of file matching a wildcard pattern.
unlink()
删除给定的文件名(如果成功则返回).
unlink()
deletes the given file name (and returns if it was successful or not).
PHP函数名称前的@
强制PHP禁止出现函数错误.
The @
before PHP function names forces PHP to suppress function errors.
通配符取决于您要删除的内容. *.*
适用于所有文件,而*.jpg
适用于jpg文件.请注意,glob
还会返回目录,因此,如果您有一个名为images.jpg
的目录,它也会同时返回它,从而导致unlink
失败,因为它仅删除文件.
The wildcard depends on what you want to delete. *.*
is for all files, while *.jpg
is for jpg files. Note that glob
also returns directories, so If you have a directory named images.jpg
, it will return it as well, thus causing unlink
to fail since it deletes files only.
is_file()
确保仅尝试删除文件.
is_file()
ensures you only attempt to delete files.
这篇关于从文件夹中删除图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!