Page 1 of 1

array_multisort to sort each filename against its date

Posted: Mon Apr 17, 2006 4:58 am
by ozzy
I dont quite understand how i would get array_multisort (http://uk2.php.net/array_multisort) to sort each filename against its date. The filenames would be collected using glob

Code: Select all

$files = glob($dir . 'Docs/files/*');
Is it possible if anyone could show me or explain to me how. Thanks in advance!

Posted: Mon Apr 17, 2006 8:19 am
by feyd
array_multisort() isn't exactly best used to sort a basic one-dimensional array like that returned from glob(). Use sort(), asort(), or usort().

Posted: Mon Apr 17, 2006 9:10 am
by John Cartwright

Code: Select all

function toArray(&$item, $key)
{
   $item = array($item => filemtime($item));
}

array_walk($files, 'toArray');

That could convert all your filesnames to an array with their filemtime(), I believe that is what your after?

Posted: Mon Apr 17, 2006 9:57 am
by ozzy
Jcart wrote:

Code: Select all

function toArray(&$item, $key)
{
   $item = array($item => filemtime($item));
}

array_walk($files, 'toArray');

That could convert all your filesnames to an array with their filemtime(), I believe that is what your after?
Thanks Jcart, that's what i was looking for. When i included that code into my script, the following error is returned:

Code: Select all

Warning: basename() expects parameter 1 to be string, array given in /host/localhost/htdocs/dir/index.php on line 55

I know what is wrong, but i dont know what to change to fix it. If you need to see a full copy of my script, i can send it to you via pm.

Posted: Mon Apr 17, 2006 10:50 am
by John Cartwright
post it here

Posted: Mon Apr 17, 2006 11:07 am
by ozzy
Here is the full thing:

Code: Select all

<?php
echo "<table width=\"100%\" border=\"0\" cellspacing=\"1\" cellpadding=\"0\">";
$location = "Docs/files/";
$thispage = "modules.php?name=Dir&file=index&";
$dir = getcwd().'/';
$difference = 30;

$start = isset($_GET['start']) ? (int)$_GET['start'] : 0;
$end = isset($_GET['end']) ? (int)$_GET['end'] : 0;
if($end <= $start) {
	$end = $start+30;
}

function smart_filemtime($filename) {
  $time = @filemtime($filename);
  if ($time) return $time;
  else return time();
} 

if (is_dir($dir)) {
    $i = 1;
    $files = glob($dir . 'Docs/files/*');
	
	function toArray(&$item, $key)
{
   $item = array($item => filemtime($item));
}

array_walk($files, 'toArray');

    foreach ($files as $path) {
    	$filename = basename($path);
        if($i >= $start && $i != $end ) {
        echo "<tr style='border:1px solid #cfcfcf;background:#fcfcfc; cursor:hand\' onMouseOver=\"this.style.background='#EEEEEE'\" onMouseOut=\"this.style.background='#FFFFFF'\" onClick=\"window.location.href='modules.php?name=Dir&file=file&page=Docs/files/$filename'\">
    <td><img src=\"modules/Dir/images/document.jpg\" width=\"16\" height=\"16\" /> <a href=\"modules.php?name=Dir&file=file&page=Docs/files/$filename\">$filename</a>
	</td>
    <td align=right><a href=\"docs/files/$filename\">Plaintext</a> | <i>$time". date ("F d Y H:i:s.", filemtime($location . $filename));
        echo "</i></td>";
        }
        if($i == $end) break;
        $i++;
    }
}


$back = (($i-$difference) < 0) ? 0 : ($i-$difference);
$forward = (($i+$difference) > $i) ? $_GET['forward'] : ($i+$difference);


echo "<b><div align=\"right\"><a href=\"\" onclick=\"history.go(-1); return false;\">Back</a>\n";
echo " | ";
echo "<a href='".$thispage."start=".$i."&end=".$i."'>Forward</a></div></b>\n";
echo "</table>";
echo "<b><div align=\"right\"><a href=\"\" onclick=\"history.go(-1); return false;\">Back</a>\n";
echo " | ";
echo "<a href='".$thispage."start=".$i."&end=".$i."'>Forward</a></div><br></b>\n";

?>

Posted: Mon Apr 17, 2006 11:18 am
by feyd

Code: Select all

$files = glob($dir . 'Docs/files/*');
       
        function toArray(&$item, $key)
{
   $item = array($item => filemtime($item));
}

array_walk($files, 'toArray');

    foreach ($files as $path) {
to

Code: Select all

$files = glob($dir . 'Docs/files/*');
       
    function toArray(&$item, $key)
    {
       $item = date("F d Y H:i:s.", filemtime($key));
    }
    $files = array_flip($files);
    array_walk($files, 'toArray');

    foreach ($files as $path => $filetime) {
and

Code: Select all

date ("F d Y H:i:s.", filemtime($location . $filename))
to

Code: Select all

$filetime

Posted: Mon Apr 17, 2006 12:47 pm
by ozzy
Hey feyd, thanks for the reply. I applied your changes to my script but it still isnt orderinf it in any particular order.

Posted: Mon Apr 17, 2006 12:50 pm
by feyd
Post your new code.

Posted: Mon Apr 17, 2006 1:17 pm
by ozzy
My code with the modifications is:

Code: Select all

<table width='100%' border=0 cellspacing=1 cellpadding=0>

<?php
$location = "Docs/files/";
$thispage = "modules.php?name=Dir&";
$dir = getcwd().'/';
$difference = 30;

$start = isset($_GET['start']) ? (int)$_GET['start'] : 0;
$end = isset($_GET['end']) ? (int)$_GET['end'] : 0;
if($end <= $start) {
	$end = $start+30;
}

function smart_filemtime($filename) {
  $time = @filemtime($filename);
  if ($time) return $time;
  else return time();
} 

if (is_dir($dir)) {
    $i = 1;
    $files = glob($dir . 'Docs/files/*');
       
    function toArray(&$item, $key)
    {
       $item = date("F d Y H:i:s.", filemtime($key));
    }
    $files = array_flip($files);
    array_walk($files, 'toArray');

    foreach ($files as $path => $filetime) {
    	$filename = basename($path);
        if($i >= $start && $i != $end ) {
        echo "<tr style='border:1px solid #cfcfcf;background:#fcfcfc; cursor:hand\' onMouseOver=\"this.style.background='#EEEEEE'\" onMouseOut=\"this.style.background='#FFFFFF'\" onClick=\"window.location.href='modules.php?name=Dir&file=file&page=Docs/files/$filename'\">
    <td><a href=\"modules.php?name=Dir&file=file&page=Docs/files/$filename\">$filename</a>
	</td>
    <td align=right><a href=\"Docs/files/$filename\">Plaintext</a> | <i>$time". $filetime;
        echo "</i></td>";
        }
        if($i == $end) break;
        $i++;
    }
}


$back = (($i-$difference) < 0) ? 0 : ($i-$difference);
$forward = (($i+$difference) > $i) ? $_GET['forward'] : ($i+$difference);


echo "<b><div align=\"right\"><a href=\"\" onclick=\"history.go(-1); return false;\">Back</a>\n";
echo " | ";
echo "<a href='".$thispage."start=".$i."&end=".$i."'>Forward</a></div></b>\n";
?>
</table>

Posted: Mon Apr 17, 2006 1:48 pm
by John Cartwright

Posted: Mon Apr 17, 2006 2:11 pm
by s.dot
Nifty, I had always been sorting an array and then using array_reverse(), I guess I overlooked arsort().

[[sorry, off topic]]

Posted: Wed Apr 19, 2006 11:51 am
by ozzy
Jcart wrote:arsort()
I was thinking...

Code: Select all

foreach($files as $file)
 {
     $file_array["name"][] = $file;
     $file_array["date"][] = filemtime($file);
 }
 asort($file_array, SORT_NUMERIC);
So i inserted it into my script. The script with above modification:

Code: Select all

location = "Docs/files/"; 
$thispage = "modules.php?name=Dir&file=indexxx2&"; 
$dir = getcwd().'/'; 
$difference = 30; 

$start = isset($_GET['start']) ? (int)$_GET['start'] : 0; 
$end = isset($_GET['end']) ? (int)$_GET['end'] : 0; 
if($end <= $start) { 
    $end = $start+30; 
} 

function smart_filemtime($filename) { 
  $time = @filemtime($filename); 
  if ($time) return $time; 
  else return time(); 
}  

if (is_dir($dir)) 
{ 
    $i = 1; 
    $files = glob($dir . 'Docs/files/*');
    foreach($files as $file) 
    { 
     $file_array[]["name"] = basename($file); 
     $file_array[]["date"] = filemtime($file); 
    } 
    asort($file_array, SORT_NUMERIC);
    foreach ($file_array as $file_info) 
    { 
        if($i >= $start && $i != $end ) 
        { 
        echo "<tr style='border:1px solid #cfcfcf;background:#fcfcfc; cursor:hand\' onMouseOver="this.style.background='#EEEEEE'" onMouseOut="this.style.background='#FFFFFF'" onClick="window.location.href='modules.php?name=Dir&file=file&page=vuns/files/".$file_info["name"]."'"> 
    <td><a href="modules.php?name=Dir&file=file&page=Docs/files/".$file_info["name"]."">".$file_info["name"]."</a> 
    </td> 
    <td align=right><a href="Docs/files/".$file_info["name"]."">Plaintext</a> | <i>".$time.date ("F d Y H:i:s.", $file_info[date]); 
        echo "</i></td>"; 
        } 
        if($i == $end) break; 
        $i++; 
    } 
} 


$back = (($i-$difference) < 0) ? 0 : ($i-$difference); 
$forward = (($i+$difference) > $i) ? $_GET['forward'] : ($i+$difference); 


echo "<b><div align="right"><a href="" onclick="history.go(-1); return false;">Back</a>\n"; 
echo " | "; 
echo "<a href='".$thispage."start=".$i."&end=".$i."'>Forward</a></div></b>\n";
But when i gave it a test all the file are aranged in some strange way, and here is a example of the order of some of the dates:
April 01 2006 17:21:12.
December 31 1969 19:00:00.
April 01 2006 17:21:11.
April 01 2006 17:21:23.
December 31 1969 19:00:00.
April 01 2006 17:21:36.
December 31 1969 19:00:00.
December 31 1969 19:00:00.
April 01 2006 17:21:36.
December 31 1969 19:00:00.
April 01 2006 17:21:39

Posted: Thu Apr 20, 2006 6:21 am
by Rivale
Looks like something wrong with the timestamp, not sure how you would fix it though.