I am writing a script that recursively gets all files in server which have been modified before a particular time along their modification dates, orders them by modification date and prints them.
The code, without ordering works fine:
<?php
try {
$rootdir = $_SERVER['DOCUMENT_ROOT'];
$raw = new RecursiveDirectoryIterator($rootdir);
$cooked = array();
$yesdate = strtotime("-5 year");
foreach(new RecursiveIteratorIterator($raw) as $file) {
if (filemtime($file) >= $yesdate) {
$cooked[] = $file;
}
}
foreach($cooked as $file) {
echo date("F d Y H:i:s.", filemtime($file)) . $file . ' ' . '<br />';
}
} catch (Exception $ex) {
echo $ex->getMessage();
}
But once I use $file as array key and filemtime($file) as value, order and attempt to loop and echo, I get 200 code but the page comes out white, can't figure out why:
<?php
try {
$rootdir = $_SERVER['DOCUMENT_ROOT'];
$raw = new RecursiveDirectoryIterator($rootdir);
$cooked = array();
$yesdate = strtotime("-5 year");
foreach(new RecursiveIteratorIterator($raw) as $file) {
if (filemtime($file) >= $yesdate) {
$cooked[$file] = filemtime($file); // $file as key , mod datetime as value
}
}
asort($cooked); // Sort
foreach($cooked as $key => $value) {
echo $key; // for example
echo $value;
//echo date("F d Y H:i:s.", filemtime($file)) . $file . ' ' . '<br />';
}
} catch (Exception $ex) {
echo $ex->getMessage();
}
What is wrong with this code?
Thank you