PHP How to Access a Folder and Delete . php Files from All Folders and Sub Folders

Asked

Viewed 40 times

-4

On my website I allow users to upload Zip files, however I am trying to scan inside the root folder and delete specialty files in all folders and subfolders of the unzipped zip as . php or . jpg files with endings that I know should not be there !

$files = glob('jogos/*.php'); // obtém os arquivos .php
foreach($files as $file){ // percorre os arquivos encontrados
  if(is_file($file))
    unlink($file); // remove o arquivo
}

However in the above code I have no success to access and scan all subfolders, the subfolders I have no control over the names nor how many are.

What I’m trying to do is access the "games" folder and sweep everything inside in search of endings . php and delete them, I have no idea where to go next

Thank you in advance for your attention

  • Has this file already been unzipped? The glob function scrolls through the contents of a directory not a file. zip

  • was yes, this in a folder called "user’s game_id " but there are subfolders, I needed to go through all subfolders and delete files . php

  • Well, if I’m not mistaken the unlink function doesn’t erase non-empty directories, I think you’ll need to access each of these directories, maybe a recursive function will solve that. But you’ll have to be very careful not to enter an infinite loop

  • As I mentioned in my comment, I believe that in your case it will be necessary a recursive function to delete the whole directory. This response from Stackoverflow in English should help you (there is an example of this function). If you want to understand recursion a little better: Recursion is the name given when a given function call occurs to itself, this occurs until a certain condition is reached. It works similarly to a repetition loop, but recursion has a higher computational cost, i.e., uses more processes

  • A suggestion in Stackoverflow, when adding new iterations to the question, it may be interesting to edit the question to make it more complete ;-)

No answers

Browser other questions tagged

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