|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[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
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Sun Oct 26 21:00:01 2025 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. http://3v4l.org/ULTrU> 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.