|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #69385 Add glob_get_base_path() function
Submitted: 2015-04-06 14:05 UTC Modified: 2015-04-06 22:58 UTC
From: bschussek at gmail dot com Assigned:
Status: Open Package: Filesystem function related
PHP Version: 5.6.8RC1 OS:
Private report: No CVE-ID: None
View Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please — but make sure to vote on the bug!
Your email address:
Solve the problem:
40 - 39 = ?
Subscribe to this entry?

 [2015-04-06 14:05 UTC] bschussek at gmail dot com
I'd love to see a glob_get_base_path() function that returns the base path of a glob following the rules used by glob(). 

The base path is the most specific directory path of a glob. Knowing the base path is useful when mapping a glob to a target directory. This mapping can be used, for example, to copy the matched files to the directory.


glob_get_base_path(string $glob[, int $flags = 0])

$glob - a glob
$flags - the glob() flags

Result: string - the base path of the glob

Usage example:


// could be stored in the application configuration
define('SOURCE_GLOB', 'css/*.css');
define('TARGET_DIR', 'public_html/css');

// runtime
$basePath = glob_get_base_path(SOURCE_GLOB);
$basePathLength = strlen($basePath);

foreach (glob($glob) as $sourcePath) {
    $targetPath = substr_replace($sourcePath, TARGET_DIR, 0, $basePathLength);

    copy($sourcePath, $targetPath);

Test script:

echo glob_get_base_path('*.css')."\n";
echo glob_get_base_path('/*.css')."\n";
echo glob_get_base_path('css/*.css)."\n";
echo glob_get_base_path('css/foo*.css)."\n";
echo glob_get_base_path('css/foo{bar,baz}.css)."\n";

Expected result:


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2015-04-06 18:52 UTC]
-Status: Open +Status: Wont fix
 [2015-04-06 18:52 UTC]
There are a number of situations this function couldn't handle, like ** from your #69382, or braces in the directory portion (like "{css,js}/*").
For everything else the regular dirname() works just fine - it's really more of a string function than a filesystem function.
 [2015-04-06 20:20 UTC] bschussek at gmail dot com
> There are a number of situations this function couldn't handle

The point of the function is not to return the directory name, but the *most specific base path*. Maybe an extension from my examples before makes it clearer:

// => css

// => /home/me

So no, dirname() is unfortunately not a solution.
 [2015-04-06 22:58 UTC]
-Status: Wont fix +Status: Open
 [2015-04-06 22:58 UTC]
I don't know, it seems like a very specific use case that doesn't really need its own brand-new function for it. Simply using two values, "css" for the known base and "/*.css" for the unknown pattern, would take care of your problem.
Maybe I'm just not seeing its full potential or something.
PHP Copyright © 2001-2021 The PHP Group
All rights reserved.
Last updated: Tue Jun 22 21:01:24 2021 UTC