Solved

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

Posted on 2014-02-16
2
346 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 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

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

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

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
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…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

730 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