Make WordPress Core

Opened 3 years ago

Closed 2 years ago

Last modified 17 months ago

#44654 closed defect (bug) (maybelater)

Use DIRECTORY_SEPARATOR instead of always using forward slash

Reported by: mnelson4 Owned by:
Milestone: Priority: normal
Severity: normal Version:
Component: Bootstrap/Load Keywords:
Focuses: Cc:


If you try to run WordPress on windows, where the directory separator is a backslash instead of a forward slash, there are a few spots where the folder paths are wrong.
We should always be using forward slashes in URLs, but filepaths should use the DIRECTORY_SEPARATOR constant. This way, the filepaths are ok on Windows machines too.

For example, wp-config-sample.php has this:

if ( !defined('ABSPATH') )
        define('ABSPATH', dirname(__FILE__) . '/');

Which, in windows, is producing a path like this: 'E:\Software\laragon\www\laragonwp/`. Notice the last slash is the wrong direction! That can cause problem when a Windows machine is trying to read a file in that filepath.

That code should instead be

if ( !defined('ABSPATH') )
        define('ABSPATH', dirname(__FILE__) . DIRECTORY_SEPARATOR);

I think these filepath issues have gone unnoticed because it's pretty rare to run WordPress directly on a Windows machine (usually folks use a virtual machine or linux subsystem on Windows 10). But I think this might become more prevalent because I'm seeing some better tools for Windows (eg https://laragon.org/) and running npm from within a virtual machine using a Windows host has problems (eg https://core.trac.wordpress.org/ticket/44271)

Change History (5)

#1 @mnelson4
3 years ago

  • Keywords close added

Oh, I actually just learned PHP is ok with using forward slashes, even when ran from Windows (see https://stackoverflow.com/questions/3554817/using-directory-separator-in-file-exists-on-windows#3554966). My problems were just in unit tests that incorrectly expected the operating system's directory separator to be used.
I ranted on this topic quite a bit on my blog: https://cmljnelson.wordpress.com/2018/07/26/which-way-do-your-slashes-face/
But basically, while I'd prefer to use DIRECTORY_SEPARATOR, I think the dye has been cast, WordPress uses forward slashes in PHP code, even for filepaths. So I think it's ok to just close this.

Last edited 3 years ago by mnelson4 (previous) (diff)

#2 @desrosj
2 years ago

  • Keywords close removed
  • Milestone Awaiting Review deleted
  • Resolution set to maybelater
  • Status changed from new to closed

Thanks @mnelson4!

I looked through Core and the only locations that use DIRECTORY_SEPARATOR currently are things like PHPMailer, ID3m and the random compatibility library. The only exception is the ms-site.php file.

I'm OK closing this to keep consistency, but marking maybelater in case something changes down the road.

#3 @desrosj
2 years ago

  • Component changed from General to Bootstrap/Load

#4 @mnelson4
2 years ago

Thanks for taking a look @desrosj!
Ya I think if this were a real issue someone else would have said so.

#5 @SergeyBiryukov
17 months ago

Last edited 17 months ago by SergeyBiryukov (previous) (diff)
Note: See TracTickets for help on using tickets.