Delete images from a folder

喜夏-厌秋 提交于 2019-11-27 18:10:21

问题


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


回答1:


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

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

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

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

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() ensures you only attempt to delete files.




回答2:


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

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



回答3:


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



回答4:


use unlink and glob function

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



来源:https://stackoverflow.com/questions/5535202/delete-images-from-a-folder

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!