• Resolved Benjamin Uzelac

    (@uzegonemad)


    Hi,

    I’m trying to use this plugin to replace missing media files after server data loss. The database is still intact, so it knows that the files used to exist.

    In classes/replacer.php line ~86, move_uploaded_file() is used. However, this will not create the folder if it doesn’t already exist. Just before that file, this plugin should check if the directory exists and if not, create the directory.

    Something like the following, just before line 86, should solve the issue.

          $targetFilePath = substr($targetFile, 0, (strrpos($targetFile, "/")));
          if (!is_dir($targetFilePath))
          {
            wp_mkdir_p($targetFilePath);
          }
    
          /* @todo See if wp_handle_sideload / wp_handle_upload can be more securely used for this */
          $result_moved = move_uploaded_file($file,$targetFile);
    
Viewing 10 replies - 1 through 10 (of 10 total)
  • Plugin Author ShortPixel

    (@shortpixel)

    Thanks for the idea @uzegonemad – we’re likely going to add this in the new plugin version! ??

    We’re getting the same error on the same line, but it doesn’t seem to be related to the folder–it’s just in /wp-content/uploads/

    Permissions are all good, so we’re stumped.

    Plugin Author ShortPixel

    (@shortpixel)

    @greatmatter – could you please try this DEV version of EMR plugin and let me know how it works for you?

    Thanks,
    Alex

    Thanks, Alex–no, it’s still getting the same error message:
    “The uploaded file could not be moved to /wp-content/uploads/[FILENAME]. This is most likely an issue with permissions, or upload failed.”

    Plugin Author ShortPixel

    (@shortpixel)

    @greatmatter – thanks for the update! Did you check the permissions?

    I did check the permissions, they are all set correctly. (Not to mention that regular uploads to the same folder work just fine)

    Plugin Author ShortPixel

    (@shortpixel)

    @greatmatter – thanks for confirming that the permissions are right. I suspect that in this case it could still be a problem with the ownership.
    This could easily explain why new uploads work fine.

    Could you please check this doc and make sure the ownership of the old files is the same as the new one?

    Thanks,
    Alex

    Ok–figured out the issue.

    In classes/replacer.php, getTargetFile() is not returning the correct path info for the destination in all cases. Sometimes, it’s returning:
    /wp-content/uploads/[FILENAME]
    rather than
    [FULL PATH]/wp-content/uploads/[FILENAME]

    As a result, it’s trying to move the file to a directory that:
    1. Doesn’t/shouldn’t exist
    2. Doesn’t/shouldn’t have permission to access
    3. Isn’t web accessible

    As a TEMPORARY and quick-and-dirty workaround, I added the following code at line 216:

    // If the target file directory isn't QUITE right...
    $upload_dir = wp_upload_dir()['path'];
    if(substr($targetFile, 0, strlen($upload_dir)) != $upload_dir) {
    
      $targetFile = $upload_dir . '/' . pathinfo($targetFile)['basename'];
    }

    This fix will likely NOT work for others–but a fix is definitely necessary.

    • This reply was modified 5 years, 4 months ago by greatmatter.
    Plugin Author ShortPixel

    (@shortpixel)

    Hey @greatmatter – we just released an update a couple of days ago, could you please check it and let us know whether it solves the problem noted by you?

    Thank you,
    Alex

    Yeah, we installed it–it doesn’t solve the problem.

Viewing 10 replies - 1 through 10 (of 10 total)
  • The topic ‘Directories not created on replace’ is closed to new replies.