1

A small problem with getting the real directory name for a file. I ran into this while investigating php-code-coverage's code.

On my system running Mac Os I have a structure like this:

/project/src/Directory/FileName.php

But if I call realpath() like this (note directory name case):

realpath("project/src/directory/FileName.php")

it gives me:

<full_name>/project/src/directory/FileName.php

So the path is valid for Mac Os, but the directory name is in wrong case, so it is not a "real" realpath.

Is there any way to get a real path with valid cases for such path "project/src/directory/FileName.php"? I've tried pathinfo() also with no luck.

deceze
  • 510,633
  • 85
  • 743
  • 889
Gino Pane
  • 4,740
  • 5
  • 31
  • 46
  • did you by any chance changed the directory folder name to Directory after you had already called realpath()? realpath uses cache thats why i am asking. – sietse85 Oct 28 '16 at 10:19
  • Are you using a case-insensitive file system (which is the default)? Then `directory` is just as real as `Directory`. If you can access the file with that path, it's real. – deceze Oct 28 '16 at 10:21
  • File system is case-insensitive, as I've written, and that is the problem in this case:) I need not only valid path, but an **exactly** valid path. – Gino Pane Oct 28 '16 at 10:26
  • @sietse85 no, no changes. – Gino Pane Oct 28 '16 at 10:27
  • "Valid" is the wrong word. It *is* all valid. What you want is the *original* capitalisation. Not sure if there's any way to do that. – deceze Oct 28 '16 at 10:43
  • As a hackaround: you could list the entries in the parent directory, which should return them using their original capitalisation; then go through each entry and see if it case-insensitively matches your entry, then use that to replace your entry. That's quite slow and cumbersome to do recursively for each part of the path obviously… – deceze Oct 28 '16 at 11:26
  • @deceze Yes, it's too performance consuming. I think about another solution, but it relates to another question: how to find out which system we use - case-sensitive or case-insensitive. It's not actually about OS, but about file systems. – Gino Pane Oct 28 '16 at 12:13

1 Answers1

-2

getcwd() can also helps you to find your working directory.

sainanky
  • 507
  • 4
  • 13