Jump to content

Archived

This topic is now archived and is closed to further replies.

Xu Wei Jie

Forward slash or back slash

Recommended Posts

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.

Share this post


Link to post
Share on other sites

However, I do not need the absolute path. I just need it to detect which platform it is on and use the appropriate slash.

Share this post


Link to post
Share on other sites
$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;
}

Share this post


Link to post
Share on other sites

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?

Share this post


Link to post
Share on other sites

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?

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

Using / will always work unless Windows changes to not allow using / which I don't see happening.

Share this post


Link to post
Share on other sites

i agree windows will never change / front slash, as it part off the internal library structure

of the operating system it self.

 

 

Share this post


Link to post
Share on other sites

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.