List files from the media in the frontend code example

Example: List files from the media in the frontend

//in code section
function onStart() {            
    $folder = '/';
    $mediaLib = \System\Classes\MediaLibrary::instance();
    // it will return us MediaLibraryItem instance     
    $files = $mediaLib->listFolderContents($folder);
    $this['mediaFiles'] = $files;
}
//Now, in markup section

<div>
    <h1> files </h1>
    <ul>
    {% for item in mediaFiles %}
        <li>
        {% if item.fileType == 'image' %}
            <img src="{{ item.publicUrl }}" height="100" width="100"/>
            <br/> {{ item.path }}
            <br/> {{ item.sizeToString() }} 
            <br/> {{ item.lastModifiedAsString() }}
        {% else %}
            {{ item.path }} 
            <br/> {{ item.sizeToString() }} 
            <br/> {{ item.lastModifiedAsString() }}
        {% endif %}
        </li>
    {% endfor %}
    </ul>
</div>

Tags:

Misc Example