Jump to content

Making the latest images in a simple gallery display first


whatwhat123

Recommended Posts

Hi,

 

Im new to php and have been trying to put together a simple image hosting site with code that ive found around the internet. I've got things working but I want the images in the gallery to display the latest uploaded first.

 

This is the code im using to display the images

 

<html>
  <head>
    <title>Slideshow</title>
    <style>
    </style>
  </head>
  <body>
    <?php
      require 'config.php';
      $offset=0;
      if(isset($_GET['offset'])) {
        $offset=$_GET['offset'];
      }
      $rowcount=1;
      $perc=round((1/$imgperrows)*100);
    ?>
    <table width="100%">
      <tbody>
        <tr>
        <?php
          $d=dir("files");
          $count=0;

          while (false !== ($entry = $d->read())) {
            if($entry!='.' && $entry!='..') {


              if($count>=$offset && $count<($offset+$imgperpage)) {
                if($useshrinked) {
                  if(file_exists('tmb/'.$tmbprefix.$entry)) {
                    echo '<td width="'.$perc.'%" align="center"><a href="images/'.$entry.'"';
                    if($launchnew) echo ' target="_blank"';
                    echo '><img src="tmb/'.$tmbprefix.$entry.'" width="80" height="80" border="0"><br>'.$entry.'</td>';
                  } else {
                    echo '<td width="'.$perc.'%" align="center"><a href="files/'.$entry.'"';
                    if($launchnew) echo ' target="_blank"';
                    echo '><img src="files/'.$entry.'" width="80" height="80" border="0"><br>'.$entry.'</td>';
                  }
                } else {
                  if(file_exists('tmb/'.$tmbprefix.$entry)) {
                    echo '<td width="'.$perc.'%" align="center"><a href="files/'.$entry.'"';
                    if($launchnew) echo ' target="_blank"';
                    echo '><img src="tmb/'.$tmbprefix.$entry.'" width="80" height="80" border="0"><br>'.$entry.'</td>';
                  } else {
                    echo '<td width="'.$perc.'%" align="center"><a href="files/'.$entry.'"';
                    if($launchnew) echo ' target="_blank"';
                    echo '><img src="noprev.gif" width="80" height="80" border="0"><br>'.$entry.'</td>';
                  }
                }
                $rowcount++;
                if($rowcount>$imgperrows) {
                  echo '</tr><tr>';
                  $rowcount=1;
                }
             }
             $count++;
          }
          }
          $d->close();
        ?>
        </tr>
      </tbody>
    </table>
    <br>
    <center>
      <input type="button" value="Previous" style="font: 11px bold arial;width:70px;"<?php if($offset==0) { ?> DISABLED<?php } ?> onClick="window.location='index.php?offset=<?php echo $offset-$imgperpage; ?>';">
       
      <input type="button" value="Next" style="font: 11px bold arial;width:70px;"<?php if($count>$offset && $count<=($offset+$imgperpage)) { ?> DISABLED<?php } ?> onClick="window.location='index.php?offset=<?php echo $offset+$imgperpage; ?>';">
    </center>

 

The uploading code im using creates a log file with upload times in it.

 

Can anyone give me some help and point me in the right direction?

Link to comment
Share on other sites

<?
function sortarrayByDate($array,$dir)
{ 
$temp = $array; 
$temp2 = array(); 
$targetdir = getcwd()."/".$dir;
foreach($temp as $key => $i){ 
	$modifyDate = floatval(date("YmdHis", (filemtime($targetdir.$i)))); 
	if($key < 10){ 
		$temp2[$i] = floatval($modifyDate."0000".$key); 
	}		
	elseif($key < 99){ 
		$temp2[$i] = floatval($modifyDate."000".$key); 
	}
	elseif($key < 999){ 
		$temp2[$i] = floatval($modifyDate."00".$key); 
	}
	elseif($key < 9999){ 
		$temp2[$i] = floatval($modifyDate."0".$key); 
	}
	else{ 
		$temp2[$i] = floatval($modifyDate.$key); 
	}
}
arsort($temp2); 
$counter = 0; 
foreach($temp2 as $key => $value){ 
	$temp[$counter] = $key; 
	$counter++; 
} 
return $temp; 
}
$targetdir = getcwd()."/docs";
$files = array();
$directory = opendir($targetdir);
while($filename = readdir($directory))
{
  if(strlen($filename) > 2)
  {
    array_push($files, $filename);
  }
}
$files = sortarrayByDate($files,'docs');
?>

 

Link to comment
Share on other sites

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.