Make WordPress Core

Opened 5 years ago

Last modified 5 years ago

#45960 new defect (bug)

plugin_basename() fails on windows path

Reported by: dovyp's profile dovyp Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version:
Component: General Keywords: reporter-feedback
Focuses: Cc:


I happened upon this little bug with plugin_basename(). Namely if a file path include the C: (Windows), it will fail. So here's a quick fix.

if ( substr( strtoupper($file), 0, 2 ) === "C:" ) {
        $file = ltrim($file, 'C:');
        $file = ltrim($file, 'c:');

If we add this as initial sterilization, it will work as expected. Otherwise, the wrong string is returned.

Change History (4)

#1 @elrae
5 years ago

You could technically run WP on a drive other than C:\, it's just the most commonly used letter for a drive on Windows. It could be f:\, z:\ or any other A-Z really. The ideal solution would take into account any character.

#2 @dovyp
5 years ago

Agreed @elrae. According to the codex though, an absolute path is required. I wonder why it's not working with a windows path?

#3 @ayeshrajans
5 years ago

I use Wordpress on Windows and never faced this problem. There even appears ( to be a test with Windows path names.

In any case, instead of a substr()/ltrim(), a simple preg_replace should do a cleaner job.

preg_replace('|^(?:[A-z]:)|', '', $file);

Above pattern will behave exactly as your snippet in the issue description.

preg_replace('|^(?:[A-z]:)([\\/])|', '$1', $file);

This pattern will also look for a forward/backwards slash in the file name (C:\), and only remove the C: part, making this behave exactly as the previous regex, but has the additional validation for proceeding slash.

#4 @pento
5 years ago

  • Keywords reporter-feedback added
  • Version trunk deleted

It'd be good to know what Windows configuration is causing this. @dovyp, can you post more information about your setup?

Note: See TracTickets for help on using tickets.