How to delete all JPG files inside a folder using PHP

Asked

Viewed 246 times

0

I have a script that imports photos and creates mini and medias thumbnails inside the registration folder and I would like that after you run this one, it erases the original photos.

The structure of the folder is 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 folder photos, but do not delete the other photos or other folders.

Delete only:

--original1.jpg
--original2.jpg

1 answer

1


Look, you can do it this way:

$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 believe it is enough. If you intend to do something more advanced than a read on the glob functions of PHP.

  • Thank you, that’s right!

  • I only changed the original* to *only, each file has a different name

Browser other questions tagged

You are not signed in. Login or sign up in order to post.