Created
November 20, 2017 14:24
-
-
Save markclowes/bfa6df9e6d69c5e5c7a5c63591ad2fcf to your computer and use it in GitHub Desktop.
PHP logicalpath()
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function logicalpath($path) { | |
// same as realpath() except keeps logical paths | |
if (!file_exists($path)) { | |
return False; | |
} | |
$filename = ''; | |
if (is_file($path)) { | |
$filename = (basename($path)); | |
$length = strlen($filename); | |
$path = substr($path, 0, -$length); | |
$filename = '/'.$filename; | |
} | |
return exec('bash -c "cd '. escapeshellarg($path) .' && pwd -L"').$filename; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I recently needed to be able to resolve the real logical path, similar to realpath() but leaving symlinks intact. I couldn't find an elegant way to do this without shelling out unfortunately. As far as I am aware it is functionally equivalent to realpath().