sort by file date

i am trying to learn php and made a video script nothing special
it list all the video files in a folder and allows you to pick the one you want to play
what i need to do is sort the files by date so that the newest video’s are displayed first
here is the scipt:
[php]<?php echo "<?xml version=\"1.0\" encoding=\"utf-8\"?".">"; ?>



surf video’s


surf
;

<?php
$path = "./moviefiles";
$showForm = 1;

if(isset($_POST['Submit'])) {

$showForm = 0;

?>

<-- --> <?php } if ($showForm == 1) { //using the opendir function $dir_handle = @opendir($path) or die("Unable to open $path"); echo ''; //running the while loop echo ''; while ($file = readdir($dir_handle)) { if($file!="." && $file!="..") echo '' . $file . ''; } echo ''; //closing the directory closedir($dir_handle); echo ''; echo ''; } ?>
</body>
</html>

[/php]any ideas

You would need to add the files to an array and sort them based on a function such as this, stat() to get the metadata from the file. Otherwise, you would need to add the files to a database and use a timestamp from the table.

Also,

[php]@opendir($path) or die("Unable to open $path");[/php]
Don't do this. The @ symbol suppresses errors and warnings. You don't want to do that, you do what to deal with the errors. Also, the usage of die() is a method of old php and should not be used for current coding practices. There are other more suitable error handling methods available now.

the book i have is out dated so not sure about other ways

Well, another way would be to just use the “glob()” function. It allows manipulation of the array and can be sorted by date
using a simple function compare. Here is a sampler…

$myvideos = glob(".avi"); // Or use glob(".*") for all files…
usort($myvideos, create_function(’$a,$b’, ‘return filemtime($a) - filemtime($b);’));

You can use glob("\folder\folder*.*") or whatever by placing your path to the files in the argument…

Hope that helps…

Sponsor our Newsletter | Privacy Policy | Terms of Service