Jump to content
php.lv forumi

Failu kārtošana pēc pievienošanas laika


Ownmen

Recommended Posts

Sveiki!

Esmu sastapies ar problēmu. No direktorijas izvelkot failus, vajag, lai tie tiktu sakārtoti pēc pievienošanas laika un tad secīgi ievadīti tabulā, bet rokoties internetā īsto līdzekli neatradu. Ceru uz Jūsu palīdzību!

 

$bilde = array();

$i = 0;
if ($dh = opendir($dir)) 
{
   while (($file = readdir($dh)) !== false) 
   {
   	 if ($file != "." && $file != "..") 
   	 {
   	 	$i++;
   	 	$bilde[$i] = $file;
   	 }
   }
   closedir($dh);

Link to comment
Share on other sites

Nezinu speciālu metodi, bet var dabūt laiku, kad pēdējo reizi modificēts. Laiks ir unixtimestamp formātā.

Vari salikt masīvā faila nosaukumus, un kā indeksus likt modificēšanas datumus. Un tad veikt kārtošanu pēc indeksiem. (funkcija ksort())

 

Tā es iztēlojos :) Gan jau, ka ir vienkāršāks veids kā to izdarīt... Just google..

Link to comment
Share on other sites

$bytime = array();

if ($dh = opendir($dir)) 
{
   while (($file = readdir($dh)) !== false) 
   {
   	 if ($file != "." && $file != "..") 
   	 {
   	 	$modtime = filemtime($file);
               $bytime[$modtime] = $file;
   	 }
   }
   closedir($dh);
}

ksort($bytime);

foreach($bytime as $time)
{
    echo $time."<br />";
}

 

Izvada tikai pēdējo bildes nosaukumu. Arī pirms ksort() izvada to pašu. Vai es neprotu datus ielikt tabulā? :/

Link to comment
Share on other sites

Paldies par palīdzību, viss sanāca. Ja kādam noder, tad kods:


if ($dh = opendir($dir)) 
{
   while (($file = readdir($dh)) !== false) 
   {
   	 if ($file != "." && $file != "..") 
   	 {
   	 	$bilde[] = $file;
   	 }
   }
   closedir($dh);
}

$time = array();
foreach ($bilde as $name)
{
$mtime = filemtime('dir/'.$name);
$time[$mtime] = $name;
}
krsort($time);

Link to comment
Share on other sites

Visa problēma iepriekšējā variantā bija tā, ka tu nenorādīji pilnu ceļu uz failu

<?
if($dh = opendir($dir)){
$files = array();
while(($file = readdir($dh)) !== false){
	if(is_file($dir.'/'.$file)){
		$files[filemtime($dir.'/'.$file)] = $file;
	}
}
closedir($dh);
ksort($files);
print_r($files);
}
?>

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...