?
Solved

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

Posted on 2014-02-16
2
Medium Priority
?
356 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
[X]
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
2 Comments
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 2000 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

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 …
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses

752 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