Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I define the DIRECTORY_SEPARATOR for both Windows and Linux platforms?

Tags:

Now I create a small PHP Application, here I have problem for using file path, because in Windows use this type location C:\Some\Location\index but in Linux /www/app/index so when I define the path using this / but when the application run in window machine it should be problem for this /.

So here I want to define the DIRECTORY_SEPARATOR both Windows and Linux platform.

like image 262
useCase Avatar asked Jul 08 '11 01:07

useCase


People also ask

What is Directory_separator?

“what is directory_separator in php” Code Answer In Windows it's \ in Linux it's /. DIRECTORY_SEPARATOR is constant with that OS directory separator. Use it every time in paths. In you code snippet we clearly see bad practice code. If framework/cms are widely used it doesn't mean that it's using best practice code.

Which is the correct constant for a platform independent directory separator?

If you prefer to hard-code the directory separator character, you should use the forward slash ( / ) character. It is the only recognized directory separator character on Unix systems, as the output from the example shows, and is the AltDirectorySeparatorChar on Windows.

Which is the correct constant for a platform independent directory separator Python?

In Windows you can use either \ or / as a directory separator.


2 Answers

PHP accepts both \ and / as valid path separators in all OS. So just use / in your code

like image 66
zerkms Avatar answered Sep 19 '22 00:09

zerkms


For convenience you can write define a shorter constant:

DEFINE('DS', DIRECTORY_SEPARATOR);  

and then write your path as:

$path = 'www'.DS.'app'.DS.'index';  

Or do I not understand your question?

like image 36
shelhamer Avatar answered Sep 21 '22 00:09

shelhamer