How to delete all jpg files inside a PHP folder

Asked

Viewed 902 times

1

I have several folders with images that I need that, by PHP, it deletes all files. JPG before starting a particular function, without deleting the subfolders inside.

How can I delete JPG without deleting subfolders?

The briefcase is like this:

imagem/01.JPG
imagem/02.JPG
imagem/original/01.JPG
imagem/original/02.JPG

I want to delete only the first 2 "image/01.JPG and image/02.JPG"

  • The files are also in these subfolders?

  • yes, I changed the question showing how the folder is

  • https://answall.com/questions/56228/como-apagar-arquivos-dentro-de-uma-pasta

2 answers

6


You can do it like this:

array_map('unlink', glob("caminho/completo/*.JPG"));

The glob will return an array of all possible paths (referring to the . JPG files inside the specified folder).

The array_map will apply the function unlink on each element of the array.

The unlink will remove the file corresponding to the path.

Source

3

You can use the glob as @Juniornunes suggested, yet glob is case-sensitive (differs capital letters from minuscules), ie if you have files like this:

  • 1.JPG
  • 2.Jpg
  • 3.jpg

Only the 1.JPG shall be excluded, that is to say 2.Jpg and 3.jpg will be maintained, which I believe is not what you want.

the ideal would be to do so:

array_map('unlink', glob('images/*.[Jj][Pp][Gg]'));

With the [Jj][Pp][Gg] it will recognize the independent extension if it has any uppercase or minuscule letter.

  • 2

    Great, now that I noticed that the glob was case-sensitive, thanks!!! + 1

  • @Guilhermenascimento, referring to my answer, confuse, I thought the author wanted to graze all jpg, including the sub-folder (as I understood by the comment in the question). About the glob and preg_match, this is necessary when you want to filter the internal files as well.

  • @Valdeirpsr, I don’t think I understand. Looking at its code its function was recursive, so it is glob called within glob (for each existing folder), which could simplify the use of foreach(glob("{$dir}/*") as &$file) for foreach(glob("{$dir}/*.[Jj][Pp][Gg]") as &$file), or I’m wrong?

  • @Guilhermenascimento, this second way, works when he wants to delete only the files from a folder (ignoring subfolders). If he wanted to delete all files (including subfolders), the ideal is to use the glob to list everything (files and folders) and then the preg_match to check only the extension. In the case of this second, does not work to capture the files recursively, because it will return only the "jpg" files, without the subfolders.

  • @Valdeirpsr understood, it’s true, but it can be very simple to use GLOB_ONLYDIR and merge two globs, one from jpg and the other from recursion only. From any outside your regex can cause problems, I think it would be better to preg_match("/\.{$ext}$/i", $file). Overall I understood your proposal. Thank you for clarifying the/

Browser other questions tagged

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