Jump to content

filectime function to display latest added image in a folder....pls help


Recommended Posts

hi gurus!

 

i am back for another question regarding filectime function...

 

here's what i got so far...this code displays images from a folder with  pagination(by mr.russel)

 

now, what i wanted is to display the latest added image at the top or the first image among the four images to be displayed.

 

 

<?php

$page = $_GET['page'];
$rpp = 2;

if($page==""){
	$page = 1;
}

$dirArr = scandir('comments/Birthday/');
function rem_invalid($arr) {
	foreach ($arr as $k => $v) {
		if (stristr($v,'.gif') === false) unset($arr[$k]);
	}
}
$dirArr = preg_grep("/^.*\.gif$/i",$dirArr);
if ($c = count($dirArr)) {
	$dirArr = array_combine(range(0,count($dirArr) - 1),$dirArr);
	$numOfPages = ceil($c / $rpp);
	for ($i = (($page - 1) * $rpp); $i < min($c,($page * $rpp)); $i++) {

		$filenames = $dirArr[$i];

		//$filename = filectime($path/$filenames); <<< this is wrong...what is right way for this?

		echo "<img src='comments/Birthday/$filenames' width='200'><br>";
		echo "<input type='text' value='$filename'><br><br>";
	}
}
echo "<br /><br />";


for ($i = 1; $i <= $numOfPages; $i++) {
  		
		echo "<a href='view_pix.php?page=$i'>";
			echo " ".$i." ";
		echo "</a>";

	}

?>

tnx in advanced!

Try this

<?php
$dirArr = array_combine(range(0,count($dirArr) - 1),$dirArr);

$dirArr2 = array();
foreach($dirArr as $file)
{
$key = $filemtime($file);
$dirArr2[$key] = $file;
}
krsort($dirArr2);
$dirArr = array_values($dirArr2)
unset(dirArr2);


$numOfPages = ceil($c / $rpp);
//..
?>

replace your posted code with

 

<?php
   $page = $_GET['page'];
   $rpp = 2;
   
   if($page==""){
      $page = 1;
   }
   
   $dirArr = scandir('comments/Birthday/');
   function rem_invalid($arr) {
      foreach ($arr as $k => $v) {
         if (stristr($v,'.gif') === false) unset($arr[$k]);
      }
   }
   $dirArr = preg_grep("/^.*\.gif$/i",$dirArr);
   if ($c = count($dirArr)) {
      $dirArr = array_combine(range(0,count($dirArr) - 1),$dirArr);
//above is your code


$dirArr2 = array();
foreach($dirArr as $file)
{
$key = $filemtime($file);
$dirArr2[$key] = $file;
}
krsort($dirArr2);
$dirArr = array_values($dirArr2)
unset(dirArr2);
//below is your code

      $numOfPages = ceil($c / $rpp);
      for ($i = (($page - 1) * $rpp); $i < min($c,($page * $rpp)); $i++) {
         
         $filenames = $dirArr[$i];
         
         //$filename = filectime($path/$filenames); <<< this is wrong...what is right way for this?
         
         echo "<img src='comments/Birthday/$filenames' width='200'><br>";
         echo "<input type='text' value='$filename'><br><br>";
      }
   }
   echo "<br /><br />";

   
   for ($i = 1; $i <= $numOfPages; $i++) {
        
         echo "<a href='view_pix.php?page=$i'>";
            echo " ".$i." ";
         echo "</a>";
         
      }

?>

This thread is more than a year old. Please don't revive it unless you have something important to add.

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.   Restore formatting

  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.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.