Hi all i've got a directory where php creates a txt file every time someone update his stock. i create file txt like this: customer-last-update-stock-05-06-2008---10-29-42.txt i read the files in the directory with this script: if ($handle = opendir('/web/htdocs/directory/home/customer/update_stock/')) { while (false !== ($file = readdir($handle))) { if ($file != "." && $file != "..") { echo "<a href=\"../customer/update_stock/$file\" target=\"_blank\">$file</a><br>\n"; } } closedir($handle); } PHP: now my question is.. how can i order and print the files by last mod time?? there is any way? hope you can help bye bye
Try reading about fopen() PHP: . By the way, Why don't you use MySQL database. It's much better on storing changes than a TXT file.
I agree, mysql would be better. But, if you want to do with files and you are on unix, you can also execute a shell command for 'ls -r' or 'ls -rt' depending on which order you want.
http://forums.digitalpoint.com/showthread.php?t=925117 <?php $show = 0; // Leave as 0 for all $dir = ''; // Leave as blank for current if($dir) chdir($dir); $files = glob( '*.{html,php,php4,txt}', GLOB_BRACE ); usort( $files, 'filemtime_compare' ); function filemtime_compare( $a, $b ) { return filemtime( $b ) - filemtime( $a ); } $i = 0; foreach ( $files as $file ) { ++$i; if ( $i == $show ) break; echo $file . ' - ' . date( 'D, d M y H:i:s', filemtime($file) ) . '<br />' . "\n"; /* This is the output line */ } PHP:
thank you all i have found this solution $arrayfile = Array(); function ordina($file1,$file2) { $tempo1 = filectime($file1); $tempo2 = filectime($file2); return ($tempo1 < $tempo2) ? 1 : -1; } if ($handle = opendir('/web/htdocs/domain/home/customer/update_stock/')) { while (false !== ($file = readdir($handle))) { if ($file != "." && $file != "..") { $arrayfile[]='../customer/update_stock/'.$file; } } closedir($handle); usort($arrayfile,'ordina'); //print_r($arrayfile); foreach($arrayfile as $txt) { echo "<a href=\"$txt\" target=\"_blank\">".str_replace("../customer/update_stock/","",$txt)."</a><br>\n"; } } clearstatcache(); PHP: