DIRECTORY_SEPARATOR vs Hard-Coded Slash

This is more of a comment than it is a question.

Typically my development sites run on a desktop PC under Linux and Apache so I’ve always just hard-coded slashes but I’m in the midst of completely reprogramming everything to more up-to-date code.

So far so good until I decided to set up my Windows 11 laptop as a stand-by in case I have time when not actually at my office. Converting the PC to Ubuntu is not possible so I set it up with Apache and all the plug-ins and modules that my sites need but am having difficulty. There is programming that generates paths and on Linux it does so perfectly as (for example)

/internals/categories/test_array.php

but on the Windows system, the same path comes out as:

\\internals\categories\test_array.php

I am not sure why the triple slashes as there is nothing in the programming to do that but more importantly, as I’m running Apache, I would have expected DIRECTORY_SEPARATOR to use the appropriate slash for it rather than the one for the base operating system.

The bottom line, it appears that using DIRECTORY_SEPARATOR was a mistake on an Apache Web server!

When you run php on windows, php converts a path like - /internals/categories/test_array.php to \internals\categories\test_array.php when it makes an operating system call that references the file system, so you should always be able to use / and not need to make any changes when switching between operating systems.

Thank you. As far as I can ascertain, I do need to use / for URLs but for absolute paths being used in includes, for example, it needs to match the operating system.

Both of the following absolute file system paths work for me in windows -

require '/xampp/htdocs/keep/phpinfo.php';

require $_SERVER['DOCUMENT_ROOT'] . '/keep/phpinfo.php';

Windowss php perfectly “understands” the forward slash for any file operation.
You can hardcode it for programming need and keep the php directory seperator just for outputs to the user, so you won’t confuse him.

Either way, if there is a double slash at the root of your path, it is generated somewhere in your code.
PHP doesn’t just invent an extra slash.

mind sharing your code?

Cheers

There are instances where the absolute path for the specific OS is needed, such as:

$draw->setFont(‘C:\public_html\www\truetype\timesbd.ttf’);

This will not work when using the Linux path on a Windows system even under Apache:

$draw->setFont(’/public_html/www/truetype/timesbd.ttf’);

It is all working after I used DIRECTORY_SEPARATOR only on absolute paths and the Linux / on relative paths. As for sharing the code, it is far too extensive to post here with much of it using other custom functions.

I think the main reason your setFont() fails on Windows is that it hands the string straight to the library, which expects a native OS path (C:\…). PHP runtime can normalize /, but external libraries often can’t.

Always try to post the relevant code snippet, without it we’re guessing at what your code is actually doing.

Sponsor our Newsletter | Privacy Policy | Terms of Service