Logo Advies en zo, Meedenken en -doen

Examples of using the directory_info class

Before you start, please point this example script to a test directory. If you haven't done so already, you should see a warning displayed above ;-)

The directory variable for this example file should be a relative path based on the current directory this file is in.

When using the script IRL (in real life), it should be a path relative to the initial calling file
For instance if you have an index.php file in which you include the function file, then the (image) directory would have to be indicated relative to where the index.php file is located. If you call the script directly, then the (image) directory would have to be indicated relative to where the dirlist script is located.
So let's presume you have your main calling script in "httpdocs" and the images in the directory "site", the directory name in the script would just be "site/".

To get a good idea of what this class can do, the directory you point the script to will need to contain some files.

A good testdirectory should look something like:

	dirname/
		imagefile.jpg
		imagefile.gif
		imagefile.png
		textfile.txt
		phpfile.php
		.htaccess
		subdirectory/
			imagefile.jpg
			imagefile.png
			textfile.txt
			anotherfile.tmp

If files/directories within the criteria given were found, the class will return an array with all the information. This allows you to format the results any way you like (i.e. in a select list or displayed on a page or.... )

For lots more information on the available methods and variables in this class, have a look at the extensive documentation which was provided with this class.

Instantiating the class

To start: let's instantiate the class so it will remember results

$dirobj = new directory_info();

Example 1

A list of all image files with jpeg, jpg, gif or png extention in your directory with no information on files in subdirectories

$myList = $dirobj->get_ext_based_filelist( null, $pathtodir, true );

Results in:

get_ext_based_filelist( null, $pathtodir, true ); print '
filecount is : ' . $dirobj->fileselection_count . '

'; print_r( $myList ); // print_r( $dirobj->filelist_selection ); // does the same print '
'; print '

which is a selection against the (larger) total filelist:

'; print '
filecount is : ' . $dirobj->filecount . '

'; print_r( $dirobj->filelist ); print '
'; ?>

Example 2

Let's make the current selection (image files) even more specific by saying we only want to see files modified in the last 4 weeks.

$myList = $dirobj->get_files_modified_since( ( time() - (4 * 7 * 24 * 60 * 60 ) ), true, $pathtodir, true );

Results in:

get_files_modified_since( $comparets, true, $pathtodir, true ); print '
filecount is : ' . $dirobj->fileselection_count . '

'; print_r( $myList ); print '
'; ?>

Example 3

Let's use the original list to select the most recent file.

$myfile = $dirobj->get_most_recent_file( false );

Results in:

get_most_recent_file( false ); print '
';
	print_r( $myfile );
	print '
'; ?>

Hmm... that date doesn't look very good, let's make it a bit more readable using the class default datetimeformat:

date( $dirobj->datetime_format, $myfile['last_modified'])

which gives us:

' . date( $dirobj->datetime_format, $myfile['last_modified']) . ''; ?>

Example 4

Let's get some more information on the first 2 files (if we have 2) within the selection

for( $i=0; $i<2; $++ ) {
	$file = $dirobj->filelist_selection[$i];
	print '<p>filename is : ' . $file . '<br>';
	print 'filesize is ' . $dirobj->get_human_readable_filesize( $pathtodir . $file ) . '<br>';
	print 'last modified date is ' . $dirobj->get_human_readable_lastmod( $dirobj->last_path . $file ) . '<br>';
	print 'last access date is ' . $dirobj->get_human_readable_lastacc( $pathtodir . $file, 'l j F Y \a\t g:i a' ) . '<br>';
	print 'file permissions are ' . $dirobj->get_human_readable_file_permissions( $pathtodir . $file ) . '<br>';
	print 'file owner is ' . $dirobj->get_file_owner( $pathtodir . $file ) . '</p>';
}

Results in:

fileselection_count > 2 ) ? 2 : $dirobj->fileselection_count; for( $i=0; $i < $max; $i++ ) { $file = $dirobj->filelist_selection[$i]; print '
filename is : ' . $file . "\n";
		print 'file size is : ' . $dirobj->get_human_readable_filesize( $pathtodir . $file ) . "\n";
		print 'file was last modified on : ' . $dirobj->get_human_readable_lastmod( $dirobj->last_path . $file ) . "\n";
		print 'file was last accessed on : ' . $dirobj->get_human_readable_lastacc( $pathtodir . $file, 'l j F Y \a\t g:i a' ) . "\n";
		print 'file permissions are : ' . $dirobj->get_human_readable_file_permissions( $pathtodir . $file ) . "\n";
		print 'file owner is : ' . $dirobj->get_file_owner( $pathtodir . $file ) . '
'; } ?>

Example 5

Ok, so maybe I want to know the total filesize of these files (based on the selection)

$dirsize = $dirobj->get_dirsize( true );

Results in:

get_dirsize( true ); print '
' . $dirsize . '
'; ?>

We can do better... we want that in a human readable format:

method 1:
$rdb_dirsize = $dirobj->get_human_readable_dirsize( true );

method 2:
$rdb_dirsize = $dirobj->human_readable_filesize( $dirsize );
	
method 1: ' . $dirobj->get_human_readable_dirsize( true ) . "\n"; print 'method 2: ' . $dirobj->human_readable_filesize( $dirsize ) . '(quicker if you already have a size)'; ?>

Oh and while we are at it, let's also get the directory size of the total list:

$rdb_dirsize = $dirobj->get_human_readable_dirsize();

Only method 1 applies as we haven't got a size yet

' . $dirobj->get_human_readable_dirsize() . ''; ?>

Example 6

Ok, now I want to show ONE sorted list of all directories and the selected files with directories at the top, sorted in reverse order

$myList = array_merge( $dirobj->get_sorted_dirlist( false), $dirobj->get_sorted_filelist( false, true ) ); '; print_r ( array_merge( $dirobj->get_sorted_dirlist( false), $dirobj->get_sorted_filelist( false, true ) ) ); print ''; ?>

Example 7

But heck.. this class can do more, you can just pass it a path to a file and get information about the file (independently of getting the file through a directory listing using this class)

For the purpose of this example, I use the first file in your filelist - but you can just point the class to any file you like.

filelist[0]; //$pathtofile = $pathtodir . 'example/example.php'; print '
filename is : ' . $pathtofile . '
'; print '
$dirobj->get_mime_content_type( $pathtofile )' . "\n\n";
	print 'mimetype is : ' . $dirobj->get_mime_content_type( $pathtofile ) . '
'; print '
$dirobj->check_file_extension( $pathtofile, \'jpg\' )' . "\n\n";
	print 'File extension check gives : ' . ( $dirobj->check_file_extension( $pathtofile, 'php' ) ? 'true' : 'false' ) . '
'; print '
$dirobj->check_file_mimetype( $pathtofile )' . "\n\n";
	print 'File mimetype check against image files (default) gives : ' . ( $dirobj->check_file_mimetype( $pathtofile ) ? 'true' : 'false' ) . '
'; print '
$dirobj->check_allowed_file( $pathtofile, \jpg\', true )' . "\n\n";
	print 'Combined check gives : ' . ( $dirobj->check_allowed_file( $pathtofile, 'php', true ) ? 'true' : 'false' ) . '
'; print '
$dirobj->get_filesize( $pathtofile )' . "\n\n";
	print 'file size is : ' . $dirobj->get_filesize( $pathtofile ) . '
'; print '
$dirobj->get_human_readable_filesize( $pathtofile )' . "\n\n";
	print 'file size is : ' . $dirobj->get_human_readable_filesize( $pathtofile ) . '
'; print '
$dirobj->get_lastmod_unixts( $pathtofile )' . "\n\n";
	print 'file was last modified on : ' . $dirobj->get_lastmod_unixts( $pathtofile ) . '
'; print '
$dirobj->get_human_readable_lastmod( $pathtofile )' . "\n\n";
	print 'file was last modified on : ' . $dirobj->get_human_readable_lastmod( $pathtofile ) . '
'; print '
$dirobj->get_lastacc_unixts( $pathtofile )' . "\n\n";
	print 'file was last accessed on : ' . $dirobj->get_lastacc_unixts( $pathtofile ) . '
'; print '
$dirobj->get_human_readable_lastacc( $pathtofile, \'l j F Y \a\t g:i a\' )' . "\n\n";
	print 'file was last accessed on : ' . $dirobj->get_human_readable_lastacc( $pathtofile, 'l j F Y \a\t g:i a' ) . '
'; print '
$dirobj->get_human_readable_file_permissions( $pathtofile )' . "\n\n";
	print 'file permissions are : ' . $dirobj->get_human_readable_file_permissions( $pathtofile ) . '
'; print '
$dirobj->get_file_owner( $pathtofile )' . "\n\n";
	print 'file owner is : ' . $dirobj->get_file_owner( $pathtofile ) . '
'; print '
'; ?>