PHP Coding
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Codewalkers ForumsPHP RelatedPHP Coding

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Codewalkers Forums Sponsor:
Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here
  #1  
Old May 16th, 2008, 05:06 PM
Peuplarchie's Avatar
Peuplarchie Peuplarchie is offline
Stand
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Location: Québec
Posts: 147 Peuplarchie User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 Days 13 h 35 m 3 sec
Reputation Power: 2
Post php4 - Sort (date & size) listing of dir.

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 Code:
<?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 !
__________________
That's why we are not alone on earth, to help each other !
Let's build !

Reply With Quote
  #2  
Old May 17th, 2008, 01:52 AM
tkarkkainen's Avatar
tkarkkainen tkarkkainen is offline
Moderator
Click here for more information
 
Join Date: Apr 2007
Location: Finland
Posts: 2,320 tkarkkainen User rank is Lance Corporal (50 - 100 Reputation Level)tkarkkainen User rank is Lance Corporal (50 - 100 Reputation Level)tkarkkainen User rank is Lance Corporal (50 - 100 Reputation Level)  Folding Points: 11979 Folding Title: Novice Folder
Time spent in forums: 6 Days 8 h 50 m 37 sec
Reputation Power: 4
First of all, you have many unnecessary calls to sort(). I would rewrite your directory reading loop:

PHP Code:
 $dimg opendir($imgdir);
while(
$imgfile readdir($dimg))
{
    if(
in_array(strtolower(substr($imgfile,-3)),$allowed_types))
    {
        
$a_img[] = $imgfile;
        
$fmodtime[] = filemtime($imgdir($imgfile));
    } 
}

array_multisort($fmodtimeSORT_DESC$a_img); // sort both arrays so that the file that was modified last is first.
reset($fmodtime);
reset($a_img); 


That should work. I didn't test it, though. If you need to show the file modification time in a human readable format you can use date() to do that.
__________________
Fight Internet censorship!

Code:
()  ascii ribbon campaign - against html e-mail 
/\  www.asciiribbon.org   - against proprietary attachments

Reply With Quote
  #3  
Old May 17th, 2008, 01:11 PM
Peuplarchie's Avatar
Peuplarchie Peuplarchie is offline
Stand
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Location: Québec
Posts: 147 Peuplarchie User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 Days 13 h 35 m 3 sec
Reputation Power: 2
I can't make it to work, I had implemented it but here what I have done and what it do :

PHP Code:
 $val0 $_GET['folder'];
$val0 $val1;
$val0.="/";


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

array_multisort($fmodtimeSORT_DESC$a_img); // sort both arrays so that the file that was modified last is first.
reset($fmodtime);
reset($a_img); 


it give me : Warning: readdir(): supplied argument is not a valid Directory

Can you help me again?

Reply With Quote
  #4  
Old May 17th, 2008, 01:13 PM
tkarkkainen's Avatar
tkarkkainen tkarkkainen is offline
Moderator
Click here for more information
 
Join Date: Apr 2007
Location: Finland
Posts: 2,320 tkarkkainen User rank is Lance Corporal (50 - 100 Reputation Level)tkarkkainen User rank is Lance Corporal (50 - 100 Reputation Level)tkarkkainen User rank is Lance Corporal (50 - 100 Reputation Level)  Folding Points: 11979 Folding Title: Novice Folder
Time spent in forums: 6 Days 8 h 50 m 37 sec
Reputation Power: 4
That's because you're now calling your directory name variable $val0 instead of $imgdir like you previously did.

Reply With Quote
  #5  
Old May 17th, 2008, 01:27 PM
Peuplarchie's Avatar
Peuplarchie Peuplarchie is offline
Stand
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Location: Québec
Posts: 147 Peuplarchie User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 Days 13 h 35 m 3 sec
Reputation Power: 2
Now it give me this :
Warning: array_multisort(): Argument #1 is expected to be an array or a sort flag in

PHP Code:
 $imgdir $_GET['folder'];
$imgdir.="/";
  
$allowed_types = array(".png"".jpg"".jpeg"".gif");


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

array_multisort($fmodtimeSORT_DESC$a_img); // sort both arrays so that the file that was modified last is first.
reset($fmodtime);
reset($a_img);  
 echo 
'<img src="'.$imgdir.'/'.$a_img[$x].'" width="50" border="0">'

Reply With Quote
  #6  
Old May 18th, 2008, 04:12 AM
tkarkkainen's Avatar
tkarkkainen tkarkkainen is offline
Moderator
Click here for more information
 
Join Date: Apr 2007
Location: Finland
Posts: 2,320 tkarkkainen User rank is Lance Corporal (50 - 100 Reputation Level)tkarkkainen User rank is Lance Corporal (50 - 100 Reputation Level)tkarkkainen User rank is Lance Corporal (50 - 100 Reputation Level)  Folding Points: 11979 Folding Title: Novice Folder
Time spent in forums: 6 Days 8 h 50 m 37 sec
Reputation Power: 4
D'oh... this mistake was so stupid I'm ashamed. The $fmodtime line should be

PHP Code:
 $fmodtime[] = filemtime($imgdir.$imgfile); 

Last edited by tkarkkainen : May 18th, 2008 at 10:18 AM. Reason: removed an extra bracket

Reply With Quote
Reply

Viewing: Codewalkers ForumsPHP RelatedPHP Coding > php4 - Sort (date & size) listing of dir.


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway