简体   繁体   中英

Delete all images from folder except a original image in php

I want delete all images from 'Logo' folder except an original image.

eg.

demo.jpg - original image

demo_50.jpg, demo_100.jpg, demo_150.jpg - other images.

Now i want to delete all images except demo.jpg using php

You can maintain a list of file which you do not want to delete in a directory.

$dir = 'direcotry';
$keepFiles = array('demo.jpg');

foreach( glob("$dir/*") as $file ) {
    if( !in_array(basename($file), $keepFiles) )
        unlink($file);
}

Another solution to this is,

1.Move that original file from current directory say X to some other directory say Y

2.Remove the complete directory X

3.Now move back that original file from Y to X

use unlink :

 if( !in_array($file, $original) )//$file is total file in the directory
        unlink($file);

see this post also: delete image from folder PHP

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM