24

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.

useCase
  • 1,265
  • 6
  • 22
  • 41

7 Answers7

49

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

zerkms
  • 249,484
  • 69
  • 436
  • 539
  • thanks a lot, last time i was always confuse for this / and \ . – useCase Jul 08 '11 at 01:46
  • 4
    not all os'es are windows and nix according to http://stackoverflow.com/questions/625332/is-using-the-directory-separator-constant-neccessary using it is more cross platform e.g. mac os classic uses : as separator. see also e.g. http://edward.de.leau.net/is-the-php-directory_separator-constant-really-needed-20130121.html – edelwater Jan 21 '13 at 20:53
  • 1
    @edelwater: there is no reason to write your code according to requirements of unsupported 11yo OS. It's 2013 today. When you have something from nowadays - please welcome to discuss it. But referring to macos classic is just a nonsense – zerkms Jan 21 '13 at 21:13
  • @zerkms so in that case http://stackoverflow.com/questions/625332/is-using-the-directory-separator-constant-neccessary would not be the right answer? And ofcourse I understand that macos classic is old however are there NO current platforms where PHP is run that would fail? (see http://en.wikipedia.org/wiki/Path_%28computing%29) – edelwater Jan 22 '13 at 08:17
  • @edelwater: I'm here not to judge right or wrong other people answers. I've given an answer based on my experience. As a developer I'm solving the real business issues, not imaginary. So could you please provide an example of a modern and supported OS in which my advice would fail? Without tons of links to wikipedia and/or other SO answers - just a name of the OS. – zerkms Jan 22 '13 at 08:36
  • @zerkms Im looking for that also since im looking for the answer – edelwater Jan 22 '13 at 09:36
  • 1
    @edelwater: well, there is no :-) It's safe to use just `/` – zerkms Jan 22 '13 at 09:41
  • wrong statement! in Laravel i ve written `$user->photo()->create(['path'=>storage_path('rasmho/' . $filename ) ]);` and then used this path by gettinf to delete file and it just ignores and now storage directory overfilled/ I ve traced the issue an it gives incorrect slash for windows os! – CodeToLife Jul 10 '20 at 15:04
  • @CodeToLife in all php IO functions `\ ` and `/` act the same. If you used it for anything else - you obviously need to take into account requirements of the 3rd party tool. So it's not a "wrong statement", it's just you did not read it properly. – zerkms Jul 11 '20 at 00:29
23

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?

Sparkup
  • 3,686
  • 2
  • 36
  • 50
shelhamer
  • 29,752
  • 2
  • 30
  • 33
  • 1
    I wasted time by using DIRECTORY_SEPARATOR. After that "/" saves my time. Worked on Linux nicely but faced problem in windows. – Raju Ahmed Aug 06 '19 at 09:13
  • `@Raju Ahmed` , on Laravel within Eloquents it doesnt work . I didnt get why it so. But after bringing out of eloquent say : `$sLcl=DIRECTORY_SEPARATOR' and then usinj that variable it works ok now – CodeToLife Jul 10 '20 at 15:38
7

Please see PHP Predefined Constants

Maybe it's already defined in your script, try echoing DIRECTORY_SEPARATOR, see if it has any value

Neverever
  • 15,890
  • 3
  • 32
  • 50
3

PHP understands '\' and '/' as path separators, regardless on the system you're on. I prefer to use '/' (the unix way) in all of my code. When you're on a windows box and there is a need to provide a full qualified windows/DOS path I'll have this simple, non destructive function

function dosPath($path){
    return str_replace('/', '\\', $path);
}

Example:

$drive = 'C:';
$path = '/tmp/uploads';

echo dosPath($drive.$path);
echo dosPath($path);
Hans Kerkhof
  • 442
  • 3
  • 7
0

Try this for window

defined ('DS') ? null : define('DS', DIRECTORY_SEPARATOR);

define( 'SITE_ROOT',  DS . 'xampp' . DS .'htdocs' .  DS .'gallery');
stefita
  • 1,785
  • 2
  • 20
  • 35
Junaid
  • 13
  • 1
  • 2
0

Windows accepts forward slashes in most cases, so you can just use those. You can even use a mixture and it won't complain.

Make sure that your unit-test suite passes on Linux as well though!

MarkR
  • 62,604
  • 14
  • 116
  • 151
0

This is messy, would you agree?

$file = 'path' . DIRECTORY_SEPARATOR . 'to' . DIRECTORY_SEPARATOR . 'file';

$file = str_replace('/', DIRECTORY_SEPARATOR, 'path/to/file';

$file = (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') ? 'path\\to\\file' : 'path/to/file';

So just do this:

$file = 'path/to/file';

It works on Windows, Linux, Mac altogether. Backslash however only works on Windows and needs to be escaped \\ so try to avoid them. :)

To turn a file referenced by PHP on Windows into forward slahes, use str_replace. Here is an example:

$dir = str_replace('\\', '/', realpath('../'));
tim
  • 2,530
  • 3
  • 26
  • 45