I came across some PHP code I wrote way back in 2006. At the time I was doing lots of file access on both Windows and Linux with the same code and was getting sick of fixing ‘/’ vs. ‘\\’ porting issues. Its pretty straight forward simple code but thought it might be useful to others. Basically I’m just wrapping the PHP file access methods with my own that always take a Linux ‘/’ path separator and if the method determines its running on Windows it automatically converts the path to use ‘\\’.
function isWindows() { $os = php_uname('s'); return strstr($os, 'Windows'); } function convertPath($LinuxPathName) { if(isWindows()) { $basename = getcwd(); $LinuxPathName = str_replace('/','\\',$LinuxPathName); $LinuxPathName = $basename . $LinuxPathName; } return $LinuxPathName; } /** * my_fopen * * @param string $LinuxPathName Use a pathname with linux '/' convention * @param string $Permissions Use to specify file permission, example '+w' * @return int File handler descriptor */ function my_fopen($LinuxPathName, $Permissions) { $ConvertedPathName = convertPath($LinuxPathName); $handle = fopen($ConvertedPathName, $Permissions); return $handle; } /** * my_file_exists * * @param string $LinuxPathName Use a pathname with linux '/' convention * @return bool */ function my_file_exists($LinuxPathName) { $ConvertedPathName = convertPath($LinuxPathName); return file_exists($ConvertedPathName); } /** * my_filesize * * @param LinuxPathName Use a pathname with linux '/' convention * @return int */ function my_filesize($LinuxPathName) { $ConvertedPathName = convertPath($LinuxPathName); return filesize($ConvertedPathName); }