Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2014-02-16
2
Medium Priority
?
361 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

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this. Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it i…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

636 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