Solved

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

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

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 Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Both Easy and Powerful How easy is PHP? http://lmgtfy.com?q=how+easy+is+php (http://lmgtfy.com?q=how+easy+is+php)  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
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.

758 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now