Getting the directory listing using PHP's Glob Function

The other day the new developer  in my office wanted to get a directory listing using  PHP as he was writting a new plugin for WordPress. To his surprise this is really easy with the glob() function:

<?php
foreach (glob("*") as $filename) {
    echo $filename . 'size ' . filesize($filename) . 'n';
}

 

Glob allows you to use patterns similar to those when you are using the ls command in Terminal and returns the file list as an array.

Advertisements

2 thoughts on “Getting the directory listing using PHP's Glob Function

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s