Сортиране на файлове

akatsuki

Registered
Здравейте отново ! Искам да попитам как мога да взимам файлове от дадена директория като подредбата да зависи от тяхната дата на създаване. Благодря предварително!
 
filemtime(); това е функцията която трябва така вземаш датата на създраване на файла. прочети за функцията reddir


ето ти пример

<?php

if ($handle = opendir('/path/to/files')) {
echo "Directory handle: $handle\n";
echo "Files:\n";

/* This is the correct way to loop over the directory. */
while (false !== ($file = readdir($handle))) {
echo "$file\n";
}

/* This is the WRONG way to loop over the directory. */
while ($file = readdir($handle)) {
echo "$file\n";
}

closedir($handle);
}
?>
 
Код:
<?php
$dir = "files/"; // директорията, задължително с "/" накрая

function cmp($a, $b)
{
    global $dir;
    $o = strcmp(fileatime($dir.$b), fileatime($dir.$a));
    return (!$o) ? strcmp($a, $b) :  $o;
}

$files = array_diff(scandir($dir), array(".", ".."));
usort($files, "cmp");

foreach ($files as $file)
    echo $file."<br />\n";
?>
 
реших да пиша тук вместо да пускам нова тема, надявам се да няма проблеми. Въпроса ми е как да сортирам по дата този код. Кода извежда новини но искам най-новата да е най-отгоре. Ето разгледайте кода:

PHP:
<?php
   $page = 2; //брой на новини на странца
   $broi = 0; 
   $handle = opendir('posts/');
   while ($read_file = readdir($handle))
    {
        if (!is_link($read_file) and $read_file != "." and $read_file != ".." and $read_file != "thumbs.db")
        {
            $broi = $broi + 1;
        }
    }
   closedir($handle); 
   $pages=ceil($broi/$page); //пресмятаме колко ще са страниците
   $pg = (int)$_GET['page'];
   if(!$pg || !isset($pg))
   {
        $pg = 1; $n = 1;
        $handle = opendir('posts/');
        while ($read_file = readdir($handle))
                {
                    if (!is_link($read_file) and $read_file != "." and $read_file != ".." and $read_file != "thumbs.db")
                        {
                            if($n<=$page)
                                {
                                include "./posts/$read_file";
								echo "<BR />";              
								$n = $n + 1;
								}                                       
                        }
                }
   }
   else
   {
   if($pg <= 0 || $pg == NULL || $pg>$pages)
    {
        $pg = 1;
    }
   else
		{
		$limit = ($pg-1)*$page;
		$limh = 0; $i = 0;
		$handle = opendir('posts/');
			while ($read_file = readdir($handle))
			{
				if (!is_link($read_file) and $read_file != "." and $read_file != ".." and $read_file != "thumbs.db")
         {
				$limh = $limh + 1;
				if($limit < $limh and $i<$page)
				{
					include "./posts/$read_file";
					echo "<BR />"; 
					$i = $i + 1;
				}
         }
			}
		}
    }
if($pg <= $pages  and $pg != 1) 
{
 $prev = $pg - 1; 
 echo "<a href='index.php?page=$prev'>[Предишна]</a>";
}
elseif($pg == 1)
{
 echo "[Предишна]";
}
if($pg < $pages and $pg != $pages) 
{
 $next = $pg + 1; 
 echo "<a href='index.php?page=$next'>[Следваща]</a>";
}
elseif($pg == $pages)
{
 echo "[Следваща]";
}
?>
 

Back
Горе