php.net |  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
Have you experienced this issue?
Rate the importance of this bug to you:

 [2015-04-06 14:05 UTC] bschussek at gmail dot com
Description:
------------
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.

Signature:

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:

<?php

// 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:
---------------
<?php

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:
----------------
.
/
css
css
css


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2015-04-06 18:52 UTC] requinix@php.net
-Status: Open +Status: Wont fix
 [2015-04-06 18:52 UTC] requinix@php.net
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.
http://3v4l.org/ULTrU
 [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:

glob_get_base_path('css/**/*.css')
// => css

glob_get_base_path('/home/me/data{,.bak}/.settings/*.conf')
// => /home/me

So no, dirname() is unfortunately not a solution.
 [2015-04-06 22:58 UTC] requinix@php.net
-Status: Wont fix +Status: Open
 [2015-04-06 22:58 UTC] requinix@php.net
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-2018 The PHP Group
All rights reserved.
Last updated: Mon Oct 22 03:01:25 2018 UTC