...

View Full Version : Sort (date & size) listing of dir.



Peuplarchie
05-17-2008, 12:06 AM
Good day to you all,
I'm building a little image directory viewer and I want it to list the images by date they were uploaded and within that by landscape or portrait.

I would need newest on top.


Here is the code I have so far :




<?php

$val= $_GET['folder'];
$val.="/";




$imgdir = $val; // the directory, where your images are stored
$allowed_types = array('png','jpg','jpeg','gif'); // list of filetypes you want to show

$dimg = opendir($imgdir);
while($imgfile = readdir($dimg))
{
if(in_array(strtolower(substr($imgfile,-3)),$allowed_types))
{
$a_img[] = $imgfile;
sort($a_img);
reset ($a_img);
}
}

$totimg = count($a_img); // total image number

for($x=0; $x < $totimg; $x++)
{
$size = getimagesize($imgdir.'/'.$a_img[$x]);



echo "<tr><td>";
if ($size[0] > "199"){

echo '<img src="'.$imgdir.'/'.$a_img[$x].'" width="50" border="0">';
}
echo "</td</tr>";


echo "<tr><td>";
if ($size[1] > "199"){

echo '<img src="'.$imgdir.'/'.$a_img[$x].'" width="50" border="0">';
}
echo "</td></tr>";
}
?>






Can someone guide me with this please ?

Thanks !
Have a nice day !

syosoft
05-17-2008, 07:49 AM
Give this a shot - notice the server path at the top - you'll need to change that as well as the default folder path. I've also left the output/display up to you.



<?php
$images = array('landscape' => array(), 'portrait' => array());
$images_root = '/home/account/public_html/images/';
$directory = isset($_GET['folder']) && is_dir($images_root.$_GET['folder']) && is_readable($images_root.$_GET['folder']) ? $_GET['folder'].'/' : '';
$image_types = array('png', 'jpg', 'jpeg', 'gif');

foreach(glob($images_root.$directory.'*.*') as $image) :
$extension = strtolower(array_pop(explode('.', $image)));

if(!is_readable($image) || !in_array($extension, $image_types)) :
continue;
endif;

list($width, $height) = getimagesize($image);
$image_layout = $width <= $height ? 'portrait' : 'landscape';
$modified_time = filemtime($image);

$images[$image_layout][$modified_time] = array(
'path' => str_replace($images_root, '', $image),
'height' => $height,
'width' => $width,
'date' => date('Y-m-d H:i:s', $modified_time)
);
endforeach;

krsort($images['landscape']);
krsort($images['portrait']);

echo '<pre>';
print_r($images);
echo '</pre>';
?>

Peuplarchie
05-17-2008, 09:46 PM
Thanks, I have tied it and I get an error maybe you would say why :
Parse error: parse error, unexpected T_BOOLEAN_AND in.....






$images = array('landscape' => array(), 'portrait' => array());
$site_root = '/domains/p/pet.test.com/user/htdocs/';
$directory = isset($_GET['folder']) ? && is_dir($site_root.$_GET['folder']) && is_readable($site_root.$_GET['folder']) ? $_GET['folder'].'/' : 'default/';
$image_types = array('png', 'jpg', 'jpeg', 'gif');

foreach(glob($site_root.$directory.'*.*') as $image) :
$extension = strtolower(array_pop(explode('.', $image)));

if(!is_readable($image) || !in_array($extension, $image_types)) :
continue;
endif;

list($width) = getimagesize($image);
$image_layout = $width <= 199 ? 'portrait' : 'landscape';

$images[$image_layout][filemtime($image)] = str_replace($site_root, '', $image);
endforeach;

krsort($images['landscape']);
krsort($images['portrait']);

echo '<pre>';
print_r($images);
echo '</pre>';

Inigoesdr
05-17-2008, 11:17 PM
Post the exact error message(including the line number) and either the entire file, or the line the error points to, and a few previous lines.

Edit:

$directory = isset($_GET['folder']) ? && is_dir($site_root.$_GET['folder']) && is_readable($site_root.$_GET['folder']) ? $_GET['folder'].'/' : 'default/';
That is at least one error. You probably just want to remove the "?".



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum