Windows / IIS does not rename files
-
Hi
I have been struggling to get the plugin to rename files on Windows. After some debugging I have identified the root of the problem and offer the fix below.
– Windows Server 2019
– PHP 7.3.21
– WordPress 5.7.1
– Media File Rename (Free) v5.1.9
– Real Media Library (Free) v4.13.5
– Physical Custom Upload Folder for Real Media Library (Free) v1.0.5My images are organised in sub-folders off the wp-content/uploads folder, for example:
wp-content/uploads/articles wp-content/uploads/events ...
The problem arises within the
mfrh_pathinfo()
function in theclasses\api.php
file. Theget_attached_file()
function in WordPress returns the file name containing both the DIRECTORY_SEPARATOR (which is “\” in Windows) and the “/” path separator but themfrh_pathinfo()
function only checks for the DIRECTORY_SEPARATOR and therefore truncates the path before it gets to the actual file. This means the$path_parts
variable contains invalid file path references and results in the file not being found which is why it can’t be renamed.For example,
get_attached_file()
returnsC:\inetpub\wwwroot\testsite/wp-content/uploads/articles/test-image.jpg
andmfrh_pathinfo( $path, PATHINFO_DIRNAME )
returnsC:\inetpub\wwwroot
instead of returningC:\inetpub\wwwroot\site1/wp-content/uploads/articles
. Incidentally, the native PHPpathinfo()
function returns the correct path details.My suggested fix is to change the
classes\api.php
to check for both DIRECTORY_SEPARATOR and path separator, as follows:line 67: $path = rtrim( $path, '/' . DIRECTORY_SEPARATOR ); line 70: $x = max(mfrh_mb( 'strrpos', $path, '/' ), mfrh_mb( 'strrpos', $path, DIRECTORY_SEPARATOR )); line 74: $x = max(mfrh_mb( 'strrpos', $path, '/' ), mfrh_mb( 'strrpos', $path, DIRECTORY_SEPARATOR ));
I have tested this in my environment and it works.
- The topic ‘Windows / IIS does not rename files’ is closed to new replies.