TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 05-25-2008, 08:43 PM   #1 (permalink)
The Acquainted
 
Peuplarchie's Avatar
 
Join Date: May 2008
Location: Québec
Posts: 104
Thanks: 10
Peuplarchie is on a distinguished road
Application 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 !
Peuplarchie is offline  
Reply With Quote
Old 05-25-2008, 10:11 PM   #2 (permalink)
Orc
The Prestige
 
Orc's Avatar
 
Join Date: Dec 2007
Posts: 1,044
Thanks: 193
Orc is on a distinguished road
Default

Quote:
Originally Posted by Peuplarchie View Post
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.
__________________
VillageIdiot can have my babbies ;d
Orc is offline  
Reply With Quote
The Following User Says Thank You to Orc For This Useful Post:
Peuplarchie (05-26-2008)
Old 05-25-2008, 11:10 PM   #3 (permalink)
Moderateur
RegEx Guru PHP Guru Top Contributor Advanced Programmer 
 
Salathe's Avatar
 
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
Salathe is on a distinguished road
Default

Quote:
Originally Posted by Peuplarchie View Post
I need ... the last modified date, width, height and size of the files
Some useful functions:
Salathe is offline  
Reply With Quote
The Following User Says Thank You to Salathe For This Useful Post:
Peuplarchie (05-26-2008)
Old 05-26-2008, 03:59 AM   #4 (permalink)
The Acquainted
 
Peuplarchie's Avatar
 
Join Date: May 2008
Location: Québec
Posts: 104
Thanks: 10
Peuplarchie is on a distinguished road
Default

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>";





?>
__________________
That's why we are not alone on earth... let's build !
Peuplarchie is offline  
Reply With Quote
Old 05-26-2008, 08:25 AM   #5 (permalink)
Orc
The Prestige
 
Orc's Avatar
 
Join Date: Dec 2007
Posts: 1,044
Thanks: 193
Orc is on a distinguished road
Default

Quote:
Originally Posted by Peuplarchie View Post
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.
__________________
VillageIdiot can have my babbies ;d
Orc is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 11:32 PM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design