How to List the Files from a Custom Folder in WordPress?

I am new to WordPress, and need some help.

What I am trying to do is that I have created a WordPress account, and now I want to create a section in that blog which consists all my files which are uploaded on my computer on a specific folder. So whenever I change any file there, it changes over there automatically. Is this possible?

Read More

Also is there a way that I can give only read permission to my wordpress site to people with a username and password?

I am using Linux Ubuntu 12.04

Related posts

Leave a Reply

1 comment

  1. Here we go, in form of a plugin 🙂

    custom folder file listing in wordpress

    Notes

    • The media submenu is added with the function add_media_page().

    • The folder being read is wp-content/custom/, using WordPress constants WP_CONTENT_*. If you haven’t modified them, they point to the default content dir.

    • Adjust the value of $baseDir and $baseUrl.

    • Maybe, instead of linking directly to the file, you’d want to force the download or open a media player.

    • Create a new PHP file, paste the code, put in your plugins folder, activate

    Plugin

    <?php
    /*
    Plugin Name: List Files in Custom Folder
    Plugin URI: https://stackoverflow.com/q/13416177/1287812
    Description: Add a Media page where the contents of a custom folder are listed as "<a>Filename</a> - Size"
    Author: Rodolfo Buaiz
    Author URI: http://wordpress.stackexchange.com/users/12615/brasofilo
    Version: 1.0
    License: GPL
    */
    add_action( 'admin_menu', 'so_13416177_folder_menu' );
    
    function so_13416177_folder_menu() 
    {
        add_media_page( 
            'Custom Folder Media', 
            'Custom Folder', 
            'delete_plugins', 
            'so-13416177', 
            'so_13416177_display_page' 
        );
    }
    
    function so_13416177_display_page() 
    {
        $baseDir = WP_CONTENT_DIR . '/custom';
        $baseUrl = WP_CONTENT_URL . '/custom/';
        $files = array();
        
        if ( $dir = opendir( $baseDir ) ) 
        {
            while ( $file = readdir( $dir ) ) 
            {
                if ( $file != "." && $file != '..' ) 
                {
                    if ( !is_dir( $baseDir . "/" . $file ) ) 
                    {
                        // Hide files that start with a dot
                        if( !so_834303_starts_with( $file, '.' ) ) 
                        {
                            $size = so_13416177_file_size( 
                                filesize( $baseDir . "/" . $file ) 
                                );
                            $files[] = array( $file, $size );
                        }
                    }
                }
            }       
            closedir($dir);     
        }
    
        ?><div id="icon-upload" class="icon32"></div><h2>Custom Folder</h2><?php
        
        if ( empty( $files ) ) 
        {
            echo '<p>No files!</p>';
            break;
        }
        ?>              
        <table class="widefat">
            <thead>
                <tr>
                    <th>File</th>
                    <th>Size</th>
                </tr>
            </thead>
            <tfoot>
                <tr>
                    <th>File</th>
                    <th>Size</th>
                </tr>
            </tfoot>
            <tbody>
        <?php
        foreach ($files as $file) 
        {
            ?>
               <tr>
                  <td>
                    <a href="<?php echo $baseUrl.$file[0]; ?>">
                    <?php echo $file[0]; ?>
                    </a>
                 </td>
                 <td><b><?php echo $file[1]; ?></b></td>
               </tr>
            <?php
        }
        ?>
            </tbody>
        </table>
        <?php
    }
    
    // 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] );
    }
    

    PS.

    To give users read only capability, set their role to Subscriber.