Question

I'm using glob() to parse through XML files stored on my server with the code below...

$files = glob('/home/website/dataupload/*.{xml}', GLOB_BRACE);
foreach($files as $file) {
  // ...
}

The files are stored within two subdirectories following 'dataupload' and I'm trying to return the name of the first as a variable. For example, the structure could be like so...

/home/website/dataupload/brand-here/file-type/fileone.xml

I'm not concerned about the second directory ('file-type'), but need to be able to return the first directory ('brand-here') as a variable, such as $brandName.

Is this possible to do, and if so, may I receive assistance in making this happen please?

Was it helpful?

Solution

I resolved this by using the following code. It may not be the best way to do it, but it's how I got it working!

function recursiveGlob($dir, $ext) {
  foreach ($globFiles as $file) {
    $brandpath = dirname(dirname($file));
    $brandname = substr( $brandpath, strrpos( $brandpath, '/' )+1 );
  }
}

recursiveGlob('/home/website/dataupload', 'xml');

It works great, and while I'm sure it could've been done differently, it serves it's purpose! :-)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top