skip to Main Content

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?

2

Answers


  1. Chosen as BEST ANSWER

    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/>";
    }
    ?>
    

  2. You can add the following code in your function.php file.

    <?php 
    add_filter( 'ajax_query_attachments_args', 
    'wpb_show_current_user_attachments' );
    function wpb_show_current_user_attachments( $query ) {
    $user_id = get_current_user_id();
    if ( $user_id && !current_user_can('activate_plugins') && !current_user_can('edit_others_posts
    ') ) {
        $query['author'] = $user_id;
    }
    return $query;
    } 
    ?>
    

    Visit this link for more details.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search