Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Delete multiple files in PHP that share part of a file name in different directories

Posted on 2014-02-16
2
344 Views
Last Modified: 2014-02-16
Hi,

My issue is that I am working on a site that calls data from a web service and downloads the relevant information and media files relating to the original lookup and stores them locally.

However whenever a reference is flagged as deleted in the lookup I need to clear the server of all information relating to that ID. That is straight forward enough as I am able to do that using the following code.

$id = 123456

foreach (glob("local/xml/details/$id*.*") as $filename) {
    unlink($filename);
    }

Open in new window


That works brilliantly apart form the directory structure is as follows.

root
-- local
---- xml
------ details
---- media
------- images
------- pdfs
------- docs

What I need to do is create a function that looks at each of the directories within the LOCAL directory for any document that begins with the $id value and deletes the files.

Obviously I know I can just create a function that has lookup for each of the directories but that seems like a very bad way of doing it.

I know there is also the option of having the "glob" function nested within two loops, the first one going through the list and checking each ID then inside that loop running a lookup that uses an array of the paths that need to be checked to loop through each directory one at a time.

That may be the only way but I thought I would ask if there is a simpler option before I start.

Thanks.
0
Comment
Question by:Lee Redhead
2 Comments
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
ID: 39863062
you will need to first recursively get the folders, and use for each one glob(), like this:
if ( ! function_exists('glob_recursive'))
{
    // Does not support flag GLOB_BRACE        
   function glob_recursive($pattern, $flags = 0)
   {
     $files = glob($pattern, $flags);
     foreach (glob(dirname($pattern).'/*', GLOB_ONLYDIR|GLOB_NOSORT) as $dir)
     {
       $files = array_merge($files, glob_recursive($dir.'/'.basename($pattern), $flags));
     }
  return $files;
  }
}

Open in new window

php.net/glob
0
 

Author Closing Comment

by:Lee Redhead
ID: 39863140
Looks fairly straight forward. I will have a go at putting that into practice.

Thank you.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question