Zend - The PHP Company




Files and Directories

Add Code


dynamic array of directory contents  

Type: code fragment
Added by: armelino
Entered: 28/05/2002
Last modified: 06/12/2001
Rating: - (fewer than 3 votes)
Views: 8418
This code dynamically lists the (video) files in a directory, loads them into an array, sorts them by name (without the extension), and then displays them with a thumbnail link to the actual (video) file. It also displays the size of the files in kb. I wrote this after giving up on the other array and sort code that I'd seen. None of them seemed to do all of this at once. Note that the .jpg thumbnails that link to the movies all have the same prefix name (the substring is identical). This allows the link to work. A simple text link would have done the same thing.


<?
        
//get files in the current directory
        
$handle=opendir('.');
        while (
$file readdir($handle)) {
                if (
substr($file, -3) == "avi" substr($file, -3) ==
"mpg" substr($file, -3) == "mov" substr($file, -3) == "asf") {
                        
$files[] = array(name => $filesize =>
intval(filesize($file)/1024));
                }

        }
        
closedir($handle);
        
sort($files);

        
//begin the html page and table
        
echo "<html>n<head>n<meta http-equiv="content-type"
content="
text/htmlcharset=windows-1252">n<title>Files</title>n<body
bgcolor="
#ffffff">n<b><font face="arial, helvetica,
geneva">Files:</font></b><br><br>n";
        echo 
"<table bordercolor="#000000" border="1"
cellspacing="0" cellpadding="0">nt<tr>ntt<td
align
="center"><font size="2" face="arial, helvetica,
geneva"
><b>Name</b></font></td>ntt<td align="center"><font
size
="2" face="arial, helvetica,
geneva"
><b>Size</b></font></td>nt</tr>n";

        //loop through the table rows
        for (
$i = 0; $i <= count($files)-1; $i++) {
                
$len = strlen($files[$i][name]);
                
$len = $len-4;
                
$img = "images/".substr("{$files[$i][name]}", 0,
$len).".jpg";
                echo "
t<tr>ntt<td align="left"><a
href
="{$files[$i][name]}"><img border="0" src="$img"
alt="{$files[$i][name]}"></a></font></td>ntt<td
align
="center"><font size="2" face="arial, helvetica,
geneva"
>&nbsp;{$files[$i][size]} kb</font></td>nt</tr>n";
        }
        echo "
</table>n</body>n</html>";
?>


Usage Example




Rate This Script





Search



This Category All Categories