Xu Wei Jie Posted March 27, 2009 Share Posted March 27, 2009 I am not sure if this is a PHP question. Generally, files and directories in Windows use backslash in their paths. Unix and others use forward slash. Correct me if I am wrong. However, I wish to know how php functions(especially the file functions) interpret and handle the slashes. Is it platform independent? Sometimes, I do get errors when using the wrong slash. Please advise me on this. Quote Link to comment https://forums.phpfreaks.com/topic/151354-forward-slash-or-back-slash/ Share on other sites More sharing options...
Mchl Posted March 27, 2009 Share Posted March 27, 2009 When in doubt, use realpath Quote Link to comment https://forums.phpfreaks.com/topic/151354-forward-slash-or-back-slash/#findComment-794941 Share on other sites More sharing options...
Xu Wei Jie Posted March 27, 2009 Author Share Posted March 27, 2009 However, I do not need the absolute path. I just need it to detect which platform it is on and use the appropriate slash. Quote Link to comment https://forums.phpfreaks.com/topic/151354-forward-slash-or-back-slash/#findComment-794946 Share on other sites More sharing options...
Mark Baker Posted March 27, 2009 Share Posted March 27, 2009 $os = ((strpos(strtolower(PHP_OS), 'win') === 0) || (strpos(strtolower(PHP_OS), 'cygwin') !== false)) ? 'win32' : 'unix'; switch ($os) { case 'win32' : define('SLASH','/'); break; default : define('SLASH','\\'); break; } Quote Link to comment https://forums.phpfreaks.com/topic/151354-forward-slash-or-back-slash/#findComment-794966 Share on other sites More sharing options...
Xu Wei Jie Posted March 27, 2009 Author Share Posted March 27, 2009 Thanks Mark. This snippet is very neat. However, there are many values PHP_OS can take. This means I need to find out all the operating systems on how they handle slashes. However, I want php to figure out the slash to use instead of hardcoding based on PHP_OS. Is there such a feature? Anyone has a lot of experience with such file handling in PHP? Quote Link to comment https://forums.phpfreaks.com/topic/151354-forward-slash-or-back-slash/#findComment-794981 Share on other sites More sharing options...
Xu Wei Jie Posted March 27, 2009 Author Share Posted March 27, 2009 I have read this http://sg.php.net/manual/kr/function.dirname.php It states that On Windows, both slash (/) and backslash (\) are used as directory separator character. In other environments, it is the forward slash (/). So is it safe that I can assume that forward slash / can be handled on any OS in PHP? Quote Link to comment https://forums.phpfreaks.com/topic/151354-forward-slash-or-back-slash/#findComment-794998 Share on other sites More sharing options...
Xu Wei Jie Posted March 29, 2009 Author Share Posted March 29, 2009 Hi all, Could anyone tell me if it is safe that I can assume that forward slash / can be handled on any OS in PHP? Quote Link to comment https://forums.phpfreaks.com/topic/151354-forward-slash-or-back-slash/#findComment-796066 Share on other sites More sharing options...
Silverado_NL Posted March 29, 2009 Share Posted March 29, 2009 the way i do it is use the predifined constant called DIRECTORY_SEPARATOR i would redefine it to something like DS to avoid having RLY long paths lol. <?php define( 'DS' , DIRECTORY_SEPARATOR ); define( 'BASE' , __FILE__ ); $fs_path_array = explode( DS, BASE ); ?> works on both unix and windows systems. Quote Link to comment https://forums.phpfreaks.com/topic/151354-forward-slash-or-back-slash/#findComment-796079 Share on other sites More sharing options...
Xu Wei Jie Posted March 29, 2009 Author Share Posted March 29, 2009 Thanks. It is useful to know of this constant. Here is a link on discussion of using DIRECTORY_SEPARATOR. I think it is useful as well. http://old.alanhogan.com/tips/php/directory-separator-not-necessary Quote Link to comment https://forums.phpfreaks.com/topic/151354-forward-slash-or-back-slash/#findComment-796083 Share on other sites More sharing options...
corbin Posted March 29, 2009 Share Posted March 29, 2009 Using / will always work unless Windows changes to not allow using / which I don't see happening. Quote Link to comment https://forums.phpfreaks.com/topic/151354-forward-slash-or-back-slash/#findComment-796085 Share on other sites More sharing options...
redarrow Posted March 29, 2009 Share Posted March 29, 2009 i agree windows will never change / front slash, as it part off the internal library structure of the operating system it self. Quote Link to comment https://forums.phpfreaks.com/topic/151354-forward-slash-or-back-slash/#findComment-796147 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.