Skip to content

Instantly share code, notes, and snippets.

@Balamir
Created January 9, 2017 12:55
Show Gist options
  • Select an option

  • Save Balamir/ee9d33687fb3b32f32605f28e3496983 to your computer and use it in GitHub Desktop.

Select an option

Save Balamir/ee9d33687fb3b32f32605f28e3496983 to your computer and use it in GitHub Desktop.
PHP glob() with path contains brackets "[" and "]"
<?php
/**
* glob() does not normally work if the path contains brackets "[" and "]".
* For example, this will make it fail:
* $files = glob("[myfolder]/*.txt");
* No error is returned, just an empty array!
* We need to replace that brackets with [] prefix and [] suffix.
* $files = glob("[[]myfolder[]]/*.txt");
* ta da..
**/
function sd_glob( $path ) {
$path = str_replace( array( ']', '[' ), array( '[]]', '[[]' ), $path );
return glob( $path );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment