• Resolved David Herman

    (@daveherman)


    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.5

    My 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 the classes\api.php file. The get_attached_file() function in WordPress returns the file name containing both the DIRECTORY_SEPARATOR (which is “\” in Windows) and the “/” path separator but the mfrh_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() returns C:\inetpub\wwwroot\testsite/wp-content/uploads/articles/test-image.jpg and mfrh_pathinfo( $path, PATHINFO_DIRNAME ) returns C:\inetpub\wwwroot instead of returning C:\inetpub\wwwroot\site1/wp-content/uploads/articles. Incidentally, the native PHP pathinfo() 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.

    • This topic was modified 3 years, 6 months ago by David Herman.
    • This topic was modified 3 years, 6 months ago by David Herman.
Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Author Jordy Meow

    (@tigroumeow)

    Thanks a lot for debugging this ?? It’s really nice, so really, I am so thankful (and others as well).

    I feel like the search/replace is a bit rough though; I would prefer to handle this more gracefully, if that’s possible. There are functions in WordPress to strip/add directory separators and I believe we can clean the code I already wrote (which might not do this well for Windows) and avoid having search/replace at a later point. Maybe you already looked for a better solution?

    The excellent news here is that you found exactly where the plugin was failing with Windows.

    Would you mind contacting me? I would like to propose an alternative to your code, and since I don’t use Windows (much), I would like you to try this new version to make sure it works as well as yours.

    You can contact me here: https://meowapps.com/contact/

    Thanks again ??

    Thread Starter David Herman

    (@daveherman)

    I have sent you an email via your contact form. I have also rewritten the code as you suggested – let’s see if my new rewrite is similar to yours ??

    Plugin Author Jordy Meow

    (@tigroumeow)

    Thanks to @daveherman, I could include the fix in new release yesterday. It has been tested on standard servers but not on Windows server by me, but it seems okay ?? Would be awesome to know if it works for you all. Nice weekend everyone!

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Windows / IIS does not rename files’ is closed to new replies.