0

Im using the following function to get the file list from specific folders:

$files = glob("/path/to/file/*");

Sadly, when using the following folder path, glob doesn't output anything:

$files = glob("/var/www/html/dl-meta/anime/[Erai-raws] Anime name - 01~12 [720p][Multiple Subtitle]/*");

...Even tho the path is 100% valid. When copying this path to my file explorer, it works just fine (if we remove the * at the end).

Why is that? What character do I need to escape? Is there any other characters I should escape so that type of bug doesn't happen in the future?

Cheers.

2
  • Any ideas? Im completely lost here.. Commented Dec 8, 2019 at 3:47
  • Are you trying to get local computer files name? Commented Dec 8, 2019 at 4:59

1 Answer 1

1

You must mask the following characters ? * [ ] with \ if they are not supposed to have any special meaning for glob.

See special characters in the manual.

$files = glob("/var/www/html/dl-meta/anime/\[Erai-raws\] Anime name - 01~12 \[720p\]\[Multiple Subtitle\]/*");

Update:

I did some tests and found that the backslash does not work on Windows. In Windows, the backslash is directory separator and therefore cannot be used as an escape character. With a few exceptions, this expression can be used as a escape function for Linux and Windows:

$path = preg_replace('~[\[?{]~','[$0]',$path);
$files = glob($path.'/*');

This does not work on Windows with {} if the GLOB_BRACE option is activated. GLOB_BRACE for glob () is not available on some Unix / Linux systems.

Sign up to request clarification or add additional context in comments.

2 Comments

How can you escape them? I tried quotemeta(); but it also escapes characters I dont want and vice versa.. @jspit

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.