How to delete all JPG files inside a folder using PHP

0

I have a script that imports photos and creates mini and half thumbnails inside the registration folder and I would like that after it has been rolled, it deletes the original photos.

The structure of the folder looks like this:

pasta_fotos
--original1.jpg
--original2.jpg
--pasta_miniatura
----mini1.jpg
----mini2.jpg
--pasta_medio
----medio1.jpg
----medio2.jpg

What I need is that at the end of the script it deletes the original photos, ie all that are inside the photos_folder, but does not delete the other photos or other folders.

Delete only:

--original1.jpg
--original2.jpg
    
asked by anonymous 01.08.2018 / 13:57

1 answer

1

Look, you can do this:

$files = glob('pasta_fotos/original*.jpg'); // obtém as imagens que começam com o nome "original"
foreach($files as $file){ // percorre os arquivos encontrados
  if(is_file($file))
    unlink($file); // remove o arquivo
}

I think that's enough. If you want to do something more advanced than one handles the PHP functions glob.

    
01.08.2018 / 15:33