PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
Moderator: General Moderators
mjaywoods
Forum Newbie
Posts: 11 Joined: Wed Jul 12, 2006 9:32 am
Post
by mjaywoods » Wed Jul 12, 2006 9:42 am
Hi, I am having a problem trying to sort an image directory, i need to be able to sort it by filename first of all but it would help if any one could help sorting by filesize, file modifyed etc.
Here is my code so far
Code: Select all
$baseroot = ($_SERVER['DOCUMENT_ROOT']);
$imgdir = ''.$baseroot.'/test2/cms/uploaded_images/'; // the directory, where your images are stored
$allowed_types = array('png','jpg','jpeg','gif','bmp'); // 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]);
// do whatever
$halfwidth = ceil($size[0]/2);
$halfheight = ceil($size[1]/2);
$cleanFile = pathinfo($a_img[$x]);
$exten = $cleanFile['extension'];
if ($exten=="jpg")
echo "<img src='icons/jpg.gif' alt='JPG'/>";
elseif ($exten=="gif")
echo "<img src='icons/gif.gif' alt='GIF'/>";
elseif ($exten=="jpeg")
echo "<img src='icons/jpg.gif' alt='JPEG'/>";
elseif ($exten=="png")
echo "<img src='icons/png.gif' alt='PNG'/>";
elseif ($exten=="bmp")
echo "<img src='icons/bmp.gif' alt='BMP'/>";
else
echo "";
$filename = ''.$baseroot.'/test2/cms/uploaded_images/'.$a_img[$x].'';
echo $a_img[$x].' ' . filesize($filename) . ' bytes ';
echo "Last change: ".date("d/m/Y H:i",filectime($filename));
echo ' '.$size[0].' x '.$size[1].'';
echo ' <a href=img_delete.php?id='.$a_img[$x].'&accept=NO>Delete</a><br />';
}
I've had a quick search here and google but have a deadline of a week to get this project finished while learning php as well.
hope you can help
pickle
Briney Mod
Posts: 6445 Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:
Post
by pickle » Wed Jul 12, 2006 9:59 am
It looks like you've got it sorted by filename fine. Sorting it by filesize & file modified time should be interesting.
First, make an array of all the filesizes & sort them, preserving keys.
asort() should work for that. Now, just re-order your array of files so that its keys are in the same order as the filesize array. You may be able to use
array_multisort() , but to me, that functions a bit like voodoo
.
Good luck.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
mjaywoods
Forum Newbie
Posts: 11 Joined: Wed Jul 12, 2006 9:32 am
Post
by mjaywoods » Wed Jul 12, 2006 10:20 am
Ill try doing that, i guessed i had some kinda sort in there but doesn't seem to work. I think i need to bring in the images out of a directory then make an array out of them which i can sort, thats the filename done. Then like you said get the filesize etc in an array and sort making sure they stick with the images.
julian_lp
Forum Contributor
Posts: 121 Joined: Sun Jul 09, 2006 1:00 am
Location: la plata - argentina
Post
by julian_lp » Wed Jul 12, 2006 2:08 pm
as soon as you get it working, let us give it a try ,,,,
litebearer
Forum Contributor
Posts: 194 Joined: Sat Mar 27, 2004 5:54 am
Post
by litebearer » Wed Jul 12, 2006 6:43 pm
Not sure if this is of any assistance, but...
http://www.nstoia.com/toh/technical/listdir/index.php
when sorting by size, it uses the size on disk; I would think that one could sort by dimensions by using a 'hack'
ie. convert width and height to decimals
1024 x 720 becomes .10240720
800 x 600 becomes .08000600
(note the padding with zeros)
ya think? maybe?
Lite...
mjaywoods
Forum Newbie
Posts: 11 Joined: Wed Jul 12, 2006 9:32 am
Post
by mjaywoods » Fri Jul 14, 2006 4:11 am
Thanks for everyones help, i have the basics i need now, might ajax it up a bit and tidy up the coding but heres my script that gets a file list from a directory and your able to sort by name, size and date
Code: Select all
<?php
$dir = "C:\Server\Apache2\htdocs\bourne\cms\site\uploaded_images";
$directories = array();
$files = array();
$files2 = array();
$dir = (substr($dir, -1) == '/') ? substr($dir, 0, -1) : $dir;
if(is_dir($dir)){
if($handle = opendir($dir)){
while(false !== ($file = readdir($handle))){
if($file != "." && $file != ".."){
if(is_dir($dir.'/'.$file)){
$directories[$file] = filemtime($dir.'/'.$file);
} else {
$file = strtolower($file);
if(isset($_GET['size'])){
$sizing = $_GET['size'];
}
if($sizing == 'yes'){
$files[$file] = filemtime($dir.'/'.$file);
}
else
{
$files[$file] = filesize($dir.'/'.$file);
}
}
}
}
closedir($handle);
} else {
die('Could not open directory.');
}
} else {
die('Invalid directory.');
}
if($_GET['sort'] == 'alpha'){
if($_GET['mode'] == 'desc'){
krsort($files);
$highlight = 'alpha_desc';
} else {
ksort($files);
$highlight = 'alpha_asc';
}
} else {
if($_GET['mode'] == 'asc'){
asort($files, SORT_NUMERIC);
$highlight = 'date_asc';
} else {
arsort($files, SORT_NUMERIC);
$highlight = 'date_desc';
}
}
$sort_alpha_asc = ($highlight == 'alpha_asc') ? '<b>Asc</b>' : '<a href="?sort=alpha&mode=asc&size=yes">Asc</a>';
$sort_alpha_desc = ($highlight == 'alpha_desc') ? '<b>Desc</b>' : '<a href="?sort=alpha&mode=desc&size=yes">Desc</a>';
$sort_date_asc = ($highlight == 'date_asc') ? '<b>Asc</b>' : '<a href="?sort=date&mode=asc&size=yes">Asc</a>';
$sort_date_desc = ($highlight == 'date_desc') ? '<b>Desc</b>' : '<a href="?sort=date&mode=desc&size=yes">Desc</a>';
$sort_size_asc = ($highlight == 'date_asc') ? '<b>Asc</b>' : '<a href="?sort=date&mode=asc&size=no">Asc</a>';
$sort_size_desc = ($highlight == 'date_desc') ? '<b>Desc</b>' : '<a href="?sort=date&mode=desc&size=no">Desc</a>';
echo "Sort by: Date- $sort_date_asc | $sort_date_desc; Size- $sort_size_asc | $sort_size_desc; Name- $sort_alpha_asc | $sort_alpha_desc<br />\n<br />\n";
echo "<table border=\"0\">\n<tr><td></td><td><u>File</u></td><td width=\"25\"></td><td><u>Size</u></td><td width=\"25\"></td><td><u>Last Modified</u></td></tr>\n";
foreach($files as $file => $timestamp){
$cleanFile = pathinfo($file);
$exten = $cleanFile['extension'];
echo "<tr><td>";
if ($exten=="jpg")
echo "<img src='icons/jpg.gif' alt='JPG'/>";
elseif ($exten=="gif")
echo "<img src='icons/gif.gif' alt='GIF'/>";
elseif ($exten=="jpeg")
echo "<img src='icons/jpg.gif' alt='JPEG'/>";
elseif ($exten=="png")
echo "<img src='icons/png.gif' alt='PNG'/>";
elseif ($exten=="bmp")
echo "<img src='icons/bmp.gif' alt='BMP'/>";
else
echo "</td>";
echo "<td><a href=\"$dir/$file\">$file</a></td><td></td><td>";
$filesize = filesize($dir.'/'.$file);
if($filesize >= 1048576){
echo round($filesize / 1048576, 1).'MB';
} else {
echo round($filesize / 1024, 1).'kb';
}
echo '</td><td></td><td>'.date("d/m/Y H:i",filemtime($dir.'/'.$file))."</td></tr>\n";
}
echo '</table>';
?>
mjay