Delete images from a folder

后端 未结 4 632
没有蜡笔的小新
没有蜡笔的小新 2020-12-16 20:59

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

相关标签:
4条回答
  • 2020-12-16 21:25

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

    $files = glob('folder/*.jpg');
    foreach($files as $file) {
        unlink($file);
    }
    
    0 讨论(0)
  • 2020-12-16 21:29
    $images = glob("images/*.jpg");
    foreach($images as $image){
         @unlink($image);
    }
    
    0 讨论(0)
  • 2020-12-16 21:34

    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

    0 讨论(0)
  • 2020-12-16 21:38
    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.

    0 讨论(0)
提交回复
热议问题