• Hi All – looking for some guidance as I’ve spent a few days trying to figure out how best to solve this problem – I’m hoping there is a plugin or something that someone knows about…

    Essentially I’m looking for a solution that when a user uploads a file/s from their account, that the files get automatically allocated to a unique user folder in Media Library (ideally with the User ID as the folder identifier). I don’t want the user to be able to access the Media Library/Folder afterwards. I’m thinkng that I’ll use some kind of form within their account to enable them to do the upload itself (Elementor Forms or Gravity Forms if needed) but its the folder creation part that is the issue I need solving.

    For context, I will be storing all the folders (and files that they contain) on S3 and will be automatically offloading via WP Offload Media but I just need to ensure that the files are being stored on S3 in a user specific folder that I can easily find (hence ideally having User ID in the folder name)

    Any thoughts/guidance on the best way to solve this? I’m more than happy to pay for the right plugin to help me achieve that but I just want to be sure it will do the job before I start signing up for things…

    Thanks in advance

Viewing 1 replies (of 1 total)
  • $user_id = get_current_user_id();
    $dir = '/path/to/folder/';
    if (!file_exists($dir.$user_id)) {
    mkdir($dir.$user_id)', 0777, true);
    $contentOfFile = '';
    if (!file_exists($dir .'index.php')) {
    file_put_contents($dir .'index.php', $contentOfFile);
    }
    /* do the rest of the upload stuff here … */
    }


    Depending on what way you choose to build your form will determine how you use something this this. But, the idea is simple, when you attempt to upload, get the user id, check to see if the folder exists, if not, make it. Check to see if there’s a blank index file in there, if not, make it. Then continue with the usual upload process to that folder. That process will be determined by how ever you build your form so I can’t really say anything about that. The blank index file is just to prevent being able to get to the root of the folder. But if the user knows the name of the file and the URL schema, they could still get to it so you should probably use htaccess or another way to prevent accessing the folder unless it’s not really a big deal. you could always generate a random string to the folder name as well. Your call.

    ex:

    $user_id = get_current_user_id();
    $rnd = substr(str_shuffle('0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'),1,5);
    mkdir($dir.$user_id.'_'.$rnd)', 0777, true);


    that would make a folder like 182_A8WX9 just to make really hard to guess your folder structure.

Viewing 1 replies (of 1 total)
  • You must be logged in to reply to this topic.