View Full Version : Array, add file info ...
Peuplarchie
05-25-2008, 09:41 PM
Good day to you all,
I have a script here that read a directory and return the files to an array.
I need that array to also contain the last modified date, width, height and size of the files, they are images.
here's what I'm working with :
function dircont($imgdir)
{
$dir_array = array();
if (false !== ($dir = opendir($imgdir)))
{
while (false !== ($file = readdir($dir)))
{
if ($file != '.' && $file != '..')
{
$dir_array[] = $file;
}
}
return $dir_array;
}
else
{
return false;
}
}
// To print..
// $path = "/home/helpelf/helpelf-www/";
echo "<pre>";
print_r(dircont($imgdir));
echo "</pre>";
Thanks !
Take good care !
Fou-Lu
05-26-2008, 12:13 AM
You will need two functions: getimagesize and stat for this.
function dircont($imgdir)
{
$dir_array = array();
if (false !== ($dir = opendir($imgdir)))
{
while (false !== ($file = readdir($dir)))
{
if ($file != '.' && $file != '..')
{
$fInfo = array();
$fInfo[] = $file;
if (($imageInfo = @getImageSize($file)) !== false)
{
$fInfo['width'] = $imageInfo[0];
$fInfo['height'] = $imageInfo[1];
}
if (($stat = stat($file)) !== false)
{
$fInfo['mtime'] = $stat['mtime'];
// Lol, forgot to put the size in :P
$fInfo['size'] = $stat['size'];
}
$dir_array[] = $fInfo;
}
}
return $dir_array;
}
else
{
return false;
}
}
Something along those lines what you are looking for?
Peuplarchie
05-26-2008, 05:20 AM
Wow, exactly, now I need to sort it first by date, newest first and then by height within date.
Can your help me with this too ?
Inigoesdr
05-26-2008, 08:07 AM
Wow, exactly, now I need to sort it first by date, newest first and then by height within date.
Can your help me with this too ?
Are you going to try to do this yourself before asking someone else to do it for you?
Peuplarchie
05-26-2008, 08:10 AM
yes, I just need help, directions, I'm slow learning, but once it's in, it's staying !
Peuplarchie
05-26-2008, 08:11 AM
thus, my code first was alang that way he show me how, I now more where I'm going.
Fou-Lu
05-27-2008, 01:05 AM
Lol, I'm always so hasty. Inigoesdr is right, I should be helping you to learn instead of helping you by giving code.
Let me point you in the right direction. Look into using a uasort (http://ca3.php.net/manual/en/function.uasort.php). This will let you define you're own sorting method (in your case, you need to sort by a timestamp), and return the values based on that. Check in the standard usort page, I think there are examples on there - uasort only differs in that it keeps key association.
Give that a try first, then come back, we'll give you a hand if you get lost at all!
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.