John Davidson

php - Using glob with comma in path folder


I'm using glob function to get the contents of a folder. It's working fine but when the folder name looks like this Test, Folder it doesn't work and returns an empty array.

My code:

$path = 'upload/Test, Folder';

My Answer:

The issue here is that the comma in the folder name is causing glob to interpret it as two separate paths. To fix this, you can use the glob function with the GLOB_BRACE flag to handle the comma in the folder name.

Here's an updated version of your code:

$path = 'upload/Test, Folder';
print_r(glob(ABSPATH . '{' . $path . '}/*', GLOB_BRACE));

By using the GLOB_BRACE flag and enclosing the path in curly braces, glob will treat the entire path as a single entity and correctly retrieve the contents of the folder.

Rate this post

3 of 5 based on 9509 votes


© 2024 - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us