TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   General (http://www.talkphp.com/general/)
-   -   Array, add file info ... (http://www.talkphp.com/general/2844-array-add-file-info.html)

Peuplarchie 05-25-2008 08:43 PM

Array, add file info ...
 
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 :

PHP Code:

 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 !

Orc 05-25-2008 10:11 PM

Quote:

Originally Posted by Peuplarchie (Post 14864)
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 :

PHP Code:

 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 !

You need to allow the array to write inside the while loop, so that way, it writes the filemtime, etc into that array every file in the loop, same goes for the rest of it.

What I mean for that is
PHP Code:


while ( etc etc ) {

 
$contentArray[$file]['modifiedTime'] = filemtime($file);

 
// This would mean that we make a new key with the filename, and then make an array inside the filename element, ( multidemnsional array ), and inside the new array inside the file's element, it adds the modified time. you can do the same for width, height, etc.  



Maybe you need to understand a little bit more about while loops?


Also, you don't need to put else { return false; } in there, you can only have a single thing returning from a function.

By the way, you don't need if ( $file != "." & !=".." ), you can do this aswell, if ($file==".") continue, which takes out all the periods in there, this can only work in a loop.


Also, your files wont be listed, as since you don't have the return $dir_array inside the while loop, plus you need to put it in the if statement if your going to do it with the != operand.

Salathe 05-25-2008 11:10 PM

Quote:

Originally Posted by Peuplarchie (Post 14864)
I need ... the last modified date, width, height and size of the files

Some useful functions:

Peuplarchie 05-26-2008 03:59 AM

Here is where I am now :

PHP Code:


<?php


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



 
// dircont (string [directory path])
 // Returns Array of filenames in the given directory.
 // Returns FALSE on error.
 
function dircont($imgdir)
 {
     
$dir_array = array();
     if (
false !== ($dir opendir($imgdir)))
     {
         while (
false !== ($file readdir($dir)))
         {
             if (
$file != '.' && $file != '..')
             {

                 
$dir_array[] = $file;
                 
$contentArray[$file]['modifiedTime'] = filemtime($imgdir."".$file);
                 
                 
$my_image array_values(getimagesize($imgdir."".$file));
                 list(
$width$height$type$attr) = $my_image;
                 
                 
$widthArray[$file][filemtime($imgdir."".$file)]['orientation'] = $height;
             }
         }
         return 
$widthArray;
     }

 }




 
// To print..
 // $path = "/home/helpelf/helpelf-www/";
 
echo "<pre>";
 
print_r(dircont($imgdir));
 echo 
"</pre>";





?>


Orc 05-26-2008 08:25 AM

Quote:

Originally Posted by Peuplarchie (Post 14876)
Here is where I am now :

PHP Code:


<?php


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



 
// dircont (string [directory path])
 // Returns Array of filenames in the given directory.
 // Returns FALSE on error.
 
function dircont($imgdir)
 {
     
$dir_array = array();
     if (
false !== ($dir opendir($imgdir)))
     {
         while (
false !== ($file readdir($dir)))
         {
             if (
$file != '.' && $file != '..')
             {

                 
$dir_array[] = $file;
                 
$contentArray[$file]['modifiedTime'] = filemtime($imgdir."".$file);
                 
                 
$my_image array_values(getimagesize($imgdir."".$file));
                 list(
$width$height$type$attr) = $my_image;
                 
                 
$widthArray[$file][filemtime($imgdir."".$file)]['orientation'] = $height;
             }
         }
         return 
$widthArray;
     }

 }




 
// To print..
 // $path = "/home/helpelf/helpelf-www/";
 
echo "<pre>";
 
print_r(dircont($imgdir));
 echo 
"</pre>";





?>



You need to specify what $_GET['folder'] is, and since $_GET is specifically ONLY used for url vars, you would have to put in a custom url var to show the directory of files.


All times are GMT. The time now is 12:52 AM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0