Search code examples
phpwordpressdownloadupload

How can set a custom download page url for each user in wordpress?


I want my wordpress website to work like mediafire. I already have an upload page that works only for logged in users. The uploaded files are saved in: uploads/username. However I want the downloads page to be unique for every user. I need a way for the page to show only the files that are in his upload directory to be shown to him (only the files in uploads/username). Any way to do that?


Solution

  • I found a wordpress question (Getting the names of all files in a directory with PHP) from where I got most of the code and then i added the user name function.

    <?php
    $current_user = wp_get_current_user();
    $username = $current_user->user_login;
    $path = "uploads/{$username}";
    $files = scandir($path);
    foreach ($files as &$value) {
        echo "<a href='http://example.com/{$path}/".$value."' target='_blank' >".$value."</a><br/><br/>";
    }
    ?>