JustinMs66@hotmail.com Posted September 12, 2006 Share Posted September 12, 2006 right now i have a working php script that shows all the files in the directory and puts a link on it, but what i want is to not only have the file names, but after the file names (in a different form) have the file type, and then after that have the file size. and eventually i wana be able to click on "file size" and have it sort by file size. is that possible?here i my current code:[code] <?phpif ($handle = opendir('.')) { while (false !== ($file = readdir($handle))) { if ($file != "." && $file != ".." && !is_dir($file)) { echo "<a href='" . $file . "'>" . $file . "</a><br/>"; } } closedir($handle);}?>[/code] Quote Link to comment Share on other sites More sharing options...
mainewoods Posted September 12, 2006 Share Posted September 12, 2006 read all the files into an array first and then sort them according to your desired field and then loop through them and display them. Quote Link to comment Share on other sites More sharing options...
JustinMs66@hotmail.com Posted September 12, 2006 Author Share Posted September 12, 2006 ...english? lol. please explain more, i'm not as knowledgable as you in PHP Quote Link to comment Share on other sites More sharing options...
mainewoods Posted September 13, 2006 Share Posted September 13, 2006 change your code this way:[code]<?php$thefiles = array(); //initializeif ($handle = opendir('.')) { while (false !== ($file = readdir($handle))) { if ($file != "." && $file != ".." && !is_dir($file)) { //use filesystem functions to get the filesize and other attributes RTFM $thefiles[] = array('filename' => $file, 'filetype' => $getthisvalue, 'filesize' => $getthesize); //used this way, it just adds the new item to the end of the array // it is creating an array of arrays } } closedir($handle);}//at this point the $thefiles array contains all the file info//access it using 2 indexes like this echo $thefiles[0]['filetype'];//use array functions to sort $thefiles array to your pleasure RTFM//use the foreach() to cycle through the $thefiles array and echo the files and other attributes outforeach ($thefiles as $singlefile) { echo $singlefile['filename'] . ' ' . $singlefile['filetype'] . ' ' . $singlefile['filesize'] . '<br>';}?>[/code] Quote Link to comment Share on other sites More sharing options...
JustinMs66@hotmail.com Posted September 13, 2006 Author Share Posted September 13, 2006 hhmmm...when i try that it just displays the files.but like in your //coments you say://access it using 2 indexes like this [b]echo $thefiles[0]['filetype'];[/b]and i try putting in that code, but nothing happens...i'm not sure what i do with that, if anything. Quote Link to comment Share on other sites More sharing options...
mainewoods Posted September 13, 2006 Share Posted September 13, 2006 you have to use the filesystem functions to get the other file infohttp://www.php.net/manual/en/ref.filesystem.php-and put those statements before this statement, setting '$getthisvalue' and '$getthesize'[code]//use filesystem functions to get the filesize and other attributes RTFM$thefiles[] = array('filename' => $file, 'filetype' => $getthisvalue, 'filesize' => $getthesize); [/code] -use the statement above as a 'template' and add whatever number of file attributes you want into it Quote Link to comment Share on other sites More sharing options...
JustinMs66@hotmail.com Posted September 13, 2006 Author Share Posted September 13, 2006 but thats the same line of code as before...OH i think i get itbut like what do i change, the [b]'filesize'[/b] or the [b]$getthesize[/b] ? and what do i put in in pace of that? Quote Link to comment Share on other sites More sharing options...
mainewoods Posted September 13, 2006 Share Posted September 13, 2006 the $getthesize variable would be something you set before that statement using the filesystem function filesize()[code]$getthesize = filesize($file);[/code][code]$thefiles[] = array('filename' => $file, 'filetype' => $getthisvalue, 'filesize' => $getthesize); [/code] In that statement I am creating an associative array with 3 data values. The template is[code] array('thenameyougivethefield' => $theactualdatatobestored, [**repeat the format to the left as many times as you want for as many items you want in the array**]);[/code]-you would set the value the value of $theactualdatatobestored yourself before reaching this statement. Use the filesystem section of the php doc to find the functions you need.http://www.php.net/manual/en/ref.filesystem.php-good time to give yourself a class in php arrays:http://www.php.net/manual/en/language.types.array.phphttp://www.php.net/manual/en/ref.array.php Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.