Opened 6 years ago
Last modified 6 years 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: |
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)
#2
@
6 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
@
6 years 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.
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.