0

I'm having trouble with glob() which does not return the result as expected. Basically, we upgraded the webserver to Php 8.2 from Php 7.x (Xampp setup) and in the code the glob() looking for matching files in a network path. new server has access to this path same as the old server, but it does not return the result.

my glob() function set similar to follow:

$folder_path = "\\10.0.0.10\Docs\T\T(02000-02999)\T-02345";    
$file = glob($folder_path . "\*Scoping.[Pp][Dd][Ff]", GLOB_BRACE);

There is a PDF file with the name "_T-02345 Scoping.pdf" in the folder. but I get empty array.

I have tried various fixes but cannot figure out why it does not work.

Any help would be really appreciated.

Please note: The above setup working fine on old server and stopped working on new server. Old server is running on Windows server 2012 R2 + XAMPP 3.2.2 and the new server is Windows server 2019 + XAMPP 3.3.0

This question is nothing to do with single quote or double quote issue and SO detect this as a duplicate question. As explain the question this was some settings issue and cannot figure out what is not correct. The exact same code working fine on old server.

sam6640621
  • 31
  • 6
  • Use single quotes instead of double quotes. Otherwise, you need to double the backslashes. – Barmar Jul 13 '23 at 15:04
  • Use `echo $folder_path;` to see the problem. – Barmar Jul 13 '23 at 15:05
  • @Barmar - Tried single quote and no luck. did you mean echo $file? var_dump($file) return array(0). When I try with GLOB_NOCHECK flag, it returns the file path correctly and when I sticks the path to windows explorer pdf file open. – sam6640621 Jul 13 '23 at 15:20
  • No, I meant `$folder_path`. You should see that it begins with a single backslash instead of two backslashes, because you're escaping the backslash. – Barmar Jul 13 '23 at 15:22
  • Actually, even inside single quotes you have to escape backslash to make it literal. So put 4 backslashes at the beginning. – Barmar Jul 13 '23 at 15:25
  • 1
    See https://stackoverflow.com/questions/20818283/how-to-properly-escape-a-backslash-to-match-a-literal-backslash-in-single-quoted – Barmar Jul 13 '23 at 15:25
  • @Barmar -appreciate your comments here, but I'm not sure this is anything to do with the \ the example I provided shows how the path looks like but this is actually build dynamically and use chr(92) to produce the path so it is working fine with 2x chr(92) on old server Eg: chr(92).chr(92).10.0.0.10\....i think it is more do with some settings on new server. may be Apache/Php setting or something on the windows server. 2019. – sam6640621 Jul 13 '23 at 15:33
  • @sam6640621, it's still a good habit to use single quotes when you want to prevent accidental escape sequences from expanding. What happens if someone decides to use a lowercase 'T' for the subdir, since paths are not case sensitive on Windows? You'd be expanding a tab and would break the path. – Ro Achterberg Jul 13 '23 at 15:40
  • If these are Windows servers, filenames are case-insensitive, so you don't need to write `[Pp][Dd][Ff]`. What if you juse use `*Scoping.pdf`? – Barmar Jul 13 '23 at 15:42
  • FWIW On my Linux installation, the \\* doesn't work (empty array) whereas a * (single asterisk) works. – Ro Achterberg Jul 13 '23 at 15:45
  • ```$file = glob('\\\\10.0.0.10\Docs\T\T(02000-02999)\T-02345\_T-02345 Scoping.pdf', GLOB_BRACE);``` I tried this to rule out quote and case sensitivity issue. and the result is same. – sam6640621 Jul 13 '23 at 15:58
  • Have you tried it with forward slashes? – Ro Achterberg Jul 13 '23 at 15:59
  • @RoAchterberg - still no luck with * as the above look for exact file. – sam6640621 Jul 13 '23 at 16:00
  • @RoAchterberg ```var_dump(glob('//10.0.0.10/Docs/T/T(02000-02999)/T-02345/_T-02345 Scoping.pdf', GLOB_BRACE));``` this return same result array(0) – sam6640621 Jul 13 '23 at 16:04
  • 1
    What does file_exists() report? – Ro Achterberg Jul 13 '23 at 16:05
  • bool(false) - when i use var_dump(file_exist('path to file')). However exact same path work on windows explorer and open the PDF. – sam6640621 Jul 13 '23 at 16:19
  • This means Glob() cannot see this path? although server has access to it. – sam6640621 Jul 13 '23 at 16:21
  • 1
    So it's not a `glob()` issue. I suggest you debug PHP's visibility of the target drive/folder/file. Something seems to have changed in your config after the migration. – Ro Achterberg Jul 13 '23 at 16:21
  • @RoAchterberg - I'm not sure where to start. any suggestion? – sam6640621 Jul 13 '23 at 16:22
  • I'd start by seeing if you can list the contents of the network drive's root dir at all. If you can't, troubleshoot the share. If you can, try to iteratively descend into each of the subdirs of your path and see how far you get. – Ro Achterberg Jul 13 '23 at 16:26
  • Not able to reproduce your issue as no such system for me, but glob() may not work as you assume. While some users reports it works with UNC paths ([here](https://stackoverflow.com/a/74709796/367456) and [here](https://stackoverflow.com/a/43262919/367456)), when you're in trouble to formulate the string right (what most part of the discussion was about), you may end up in unknown territory quite easily ([ref](https://www.php.net/manual/en/function.glob.php#56231), [via](https://stackoverflow.com/a/17973061/367456)), that comment also notes that glob() is case-sensitive on Windows. Take care. – hakre Jul 13 '23 at 19:26
  • 1
    @hakre, he isn't even able to see the file using `file_exists()`, so my guess is it's something to do with the share/permissions/networking. – Ro Achterberg Jul 13 '23 at 20:55
  • 1
    All - thank you for all your valuable comments. special thanks to @RoAchterberg who point out the right direction to test communication with the shared drive. It turnout the problem is something to do with the Apache service. When I add Apache as a service via XAMPP, and change the run as account to an Admin, it stops accessing the network drive. When I remove the Apache run as a service it can read the remote folder. I reinstall the service again with Admin account and now it is working correctly. Looks like something has not properly set on service when I install it last time. – sam6640621 Jul 13 '23 at 22:34
  • @RoAchterberg: Yikes, I've totally not read that, the question should have been updated for such important information. Also the string in the question is still wrong. The now given answer hints to a final access-rights issue, reconfiguring the application server has fixed this. So at least some confirmation. – hakre Jul 13 '23 at 23:32

1 Answers1

0

The issue fixed after uninstall Apache service (via XAMPP) and reinstall it. I was able to access the network folder after removing the Apache run as a service (Service was runing with admin account). However, When I reinstall the service and set the run as account to same admin account as previous, it started to work.

sam6640621
  • 31
  • 6
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jul 17 '23 at 09:55