WordPress.org

Make WordPress Core

Opened 9 months ago

Last modified 9 months ago

#45960 new defect (bug)

plugin_basename() fails on windows path

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

Description

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.

<?php
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
9 months 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
9 months 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
9 months ago

I use Wordpress on Windows and never faced this problem. There even appears (https://github.com/WordPress/wordpress-develop/blob/5.0.1/tests/phpunit/tests/functions/pluginBasename.php#L39) 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
9 months 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.