1

I am creating a photo plugin for users to view their photos. The plugin works in a way that when I add a new user, a directory is created in the uploads folder inside wp-content with the user's username as the name of the folder. This is where that user's images will be stored.

Here is the code:

function create_photo_folder() {

    $upload = wp_upload_dir();
    $upload_dir = $upload['basedir'];
    $upload_dir = $upload_dir . '/USER_PHOTOS';
    if (! is_dir($upload_dir)) {
       mkdir( $upload_dir, 0700 );
    }
     $blogusers = get_users('blog_id=1&orderby=nicename');
    foreach ($blogusers as $user) {
        wp_mkdir_p($upload_dir . '/USER_PHOTOS'.$user->user_login);

    }
}

register_activation_hook( __FILE__, 'create_photo_folder' );
add_action('user_register','create_photo_folder');

The above code works fine in creating the folders. The problem I am facing is that I want to be able to fetch the contents in the folder of each user and display it to them when they log in.

I have already created a menu page called YOUR PHOTOS for all users. Now I would like to be able to retrieve the content in each folder for each user and display it to them there.

1 Answer 1

1

First an observation, using $upload_dir.'/USER_PHOTOS'.$user->user_login may lead to a mess of folders in wp-content/.

I suggest wp-content/user-photos/user-login/. And the following example considers it this way.

In your case, you already have the menu item, for completeness this is the submenu used for development:

add_action( 'admin_menu', 'wpse_75873_folder_menu' );

function wpse_75873_folder_menu() 
{
    add_media_page( 
        'User Photos Media', 
        'User Photos', 
        'edit_posts', 
        'so-13416177', 
        'wpse_75873_display_page' 
    );
}

And this the page displayed for the user with the contents of his folder, and a couple of auxiliary functions:

function wpse_75873_display_page() 
{
    echo '<div id="icon-upload" class="icon32"></div><h2>User Photos</h2>';

    $baseDir = WP_CONTENT_DIR . '/user-photos/';
    $baseUrl = WP_CONTENT_URL . '/user-photos/';

    // build user folder
    global $current_user;
    get_currentuserinfo();
    $userDir = $baseDir . $current_user->data->user_login . '/';
    $userUrl = $baseUrl . $current_user->data->user_login . '/';

    if( !is_dir( $userDir ) )
    {
        echo '<div style="margin-top:30px;font-size:2em;font-weight:bold;color:#f00">
            Folder does not exist!
            </div>';
        return;
    }

    // build array with files in user folder
    $files = array();

    if ( $dir = opendir( $userDir ) ) 
    {
        while ( $file = readdir( $dir ) ) 
        {
            if ( $file != "." && $file != '..' ) 
            {
                if ( !is_dir( $userDir . $file ) ) 
                {
                    // Hide files that start with a dot
                    if( !so_834303_starts_with( $file, '.' ) ) 
                    {
                        $size = so_13416177_file_size( 
                            filesize( $userDir . $file ) 
                            );
                        $files[] = array( $file, $size );
                    }
                }
            }
        }       
        closedir($dir);     
    } 

    if ( empty( $files ) ) 
    {
        echo '<div style="margin-top:30px;font-size:2em;font-weight:bold;color:#00f">
            No photos!
            </div>';
    }
    else
    {
        ?>          
        <table class="widefat">
            <thead>
                <tr>
                    <th style="width:20%">Thumbnail</th>
                    <th>File</th>
                    <th>Size</th>
                </tr>
            </thead>
            <tfoot>
                <tr>
                    <th>Thumbnail</th>
                    <th>File</th>
                    <th>Size</th>
                </tr>
            </tfoot>
            <tbody>
        <?php

        foreach ($files as $file) 
        {
            ?>
               <tr>
                  <td><!-- THIS IS NOT A THUMBNAIL -->
                    <?php echo '<img src="' . $userUrl . $file[0] . '" width="50">'; ?>
                  </td>
                  <td>
                    <a href="<?php echo $userUrl . $file[0]; ?>">
                    <?php echo $file[0]; ?>
                    </a>
                 </td>
                 <td><b><?php echo $file[1]; ?></b></td>
               </tr>
            <?php
        }

        ?>
            </tbody>
        </table>
        <?php
    } // end else
}

// https://stackoverflow.com/q/834303
function so_834303_starts_with( $haystack, $needle )
{
    return !strncmp( $haystack, $needle, strlen( $needle ) );
}

// http://www.php.net/manual/en/function.filesize.php#110739
function so_13416177_file_size( $o, $depth=0 ) 
{
    if( $o > 1024 ) 
        return so_13416177_file_size( $o/1024, $depth+1 );

    $unit = array( 'B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'PB', 'ZB', 'YB' );
    return sprintf( '%.01f %s', $o, $unit[$depth] );
}

The result:
user folder photos

Based on a recent Answer in StackOverflow. The code is adapted and improved.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.