Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 12-22-2012, 09:03 AM   PM User | #1
Local Hero
New Coder

 
Join Date: May 2005
Location: Utah
Posts: 58
Thanks: 6
Thanked 0 Times in 0 Posts
Local Hero is an unknown quantity at this point
Need Array to sort files

I've searched forums and I've discovered that if I want to sort the files I have in a folder by date modified the I will need an array. That makes sense. How to do it, doesn't. Will the array change the modification date when it uses the file? Here is the code I have to pull the name - date:

PHP Code:
$handle=opendir("./content/uploaded");
  {
  while ((
$file readdir($handle))!==false)
  { 
 
$file_list .= $file " - " date("F d Y .",filectime("./content/uploaded/" .$file));}

closedir($handle);
echo 
"$file_list";
    } 
How do I add an array to sort without screwing up my code?
Thanks!
Local Hero is offline   Reply With Quote
Old 12-22-2012, 03:24 PM   PM User | #2
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,662
Thanks: 4
Thanked 2,452 Times in 2,421 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
Change the $file_list from a string to an array.
PHP Code:
$aFiles = array();
while (
false !== ($file readdir($handle)))
{
    
$aFiles[$file] = filectime('./content/uploaded' $file);
}
asort($aFiles); 
The files will now be sorted by date created. From here you can create the string and the date using a foreach on the $aFiles instead. The key is your filename (which will be unique of course), and the value is the create time.

There is a way to do this with Iterators as well, although (as much as I like objects) they are a little difficult to do properly since PHP doesn't have a default comparable method on its objects.

Reading the file's name won't issue any type of touch command so the modification date will not change.
Fou-Lu is offline   Reply With Quote
Old 12-24-2012, 06:11 AM   PM User | #3
Local Hero
New Coder

 
Join Date: May 2005
Location: Utah
Posts: 58
Thanks: 6
Thanked 0 Times in 0 Posts
Local Hero is an unknown quantity at this point
Thank you, I must be missing something. I can't get the array working. Can you explain to me what I'm doing wrong? I'd like to learn.
PHP Code:
$handle=opendir("./content/uploaded");
$aFiles = array();
                  {
while ((
$file readdir($handle))!==false)
 

 
$aFiles[$file] = filectime('./content/uploaded' $file);
}
asort($aFiles);  

foreach(
$aFiles as $value2){
    echo( 
$value2 " - " date("F d Y .",filectime("./content/uploaded/" .$value2)). " <img src=./content/uploaded/" $value2 "</a>"); 
    }
    }
    }
    
closedir($handle); 
Local Hero is offline   Reply With Quote
Old 12-24-2012, 01:01 PM   PM User | #4
sorlaker
Regular Coder

 
Join Date: Dec 2009
Posts: 166
Thanks: 23
Thanked 1 Time in 1 Post
sorlaker has a little shameless behaviour in the past
One thing ur doing wrong is that the $value2 has the "filectime('./content/uploaded' . $file);" content.

U should do something like this :
PHP Code:
$handle=opendir("./content/uploaded/"); 
$aFiles = array(); 
                  { 
while ((
$file readdir($handle))!==false
  
{  
 
$aFiles[$file] = filectime('./content/uploaded/' $file); 

asort($aFiles);   

foreach(
$aFiles as $key => $value){ 
    echo( 
$key " - " date("F d Y ."$value). " <img src=./content/uploaded/" $key "</a>");  
    } 
    } 
    } 
    
closedir($handle); 
I didn't test it yet thou. Hope it works!
sorlaker is offline   Reply With Quote
Old 12-25-2012, 06:51 PM   PM User | #5
Local Hero
New Coder

 
Join Date: May 2005
Location: Utah
Posts: 58
Thanks: 6
Thanked 0 Times in 0 Posts
Local Hero is an unknown quantity at this point
Very close, thank you. But it creates another problem. $key displays the file name correctly (67023-fish.jpg), but $key to make the image path adds some characters (h ttp://www.mysite.com/flex-graphics/content/uploaded/67023-fish.jpg%3C/a) The %3C/a makes the image not appear. What would it do that? Can we fix it, or add some code to take off the last 5 haracters?
Local Hero is offline   Reply With Quote
Old 12-25-2012, 07:30 PM   PM User | #6
Local Hero
New Coder

 
Join Date: May 2005
Location: Utah
Posts: 58
Thanks: 6
Thanked 0 Times in 0 Posts
Local Hero is an unknown quantity at this point
I tried
PHP Code:
$clean substr_replace($key ,"",-4);
     echo( 
$key " - " date("F d Y ."$value). " <img src=./content/uploaded/" $clean "</a>"); 
but it just took 4 characters off of the filename and still adds %3C/a to the end making the image sourch wrong.

Last edited by Local Hero; 12-26-2012 at 04:21 AM..
Local Hero is offline   Reply With Quote
Old 12-26-2012, 03:45 PM   PM User | #7
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,662
Thanks: 4
Thanked 2,452 Times in 2,421 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
The key shouldn't have a path in it at all, but it could be a directory since you don't specify it should be for files only. Its not recursive, so it will only have the current level.

%3C is a < character. That cannot come from reading the filenames as < is an illegal character in a filename. If I recall linux will accept < and > in its filename, but should be avoided. It is unlikely this is the case here.

The HTML you have is invalid. This is malformed: <img src=./content/uploaded/" . $key . "</a>. You have to change the <img> to be single sided, and the entire <img/> tag can be wrapped with an anchor, but you cannot have an anchor as a part of the attribute in the source as you have it now. That should be using '<img src="./content/uploaded/' . $key . '" alt=""/>' and can be wrapped with the anchor if desired. You'll need to add additional code to pull out just files though, this one will let you follow directories as well.
Fou-Lu is offline   Reply With Quote
Old 12-27-2012, 10:42 AM   PM User | #8
paulinetaylor85
New Coder

 
Join Date: Oct 2012
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
paulinetaylor85 is an unknown quantity at this point
Regarding php

Hi, I want to ask you that which will be more suitable for database, hash tables or arrays?
paulinetaylor85 is offline   Reply With Quote
Old 12-27-2012, 02:28 PM   PM User | #9
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,662
Thanks: 4
Thanked 2,452 Times in 2,421 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
Not sure why you hijacked a thread in a completely different topic, but to answer your question you only have the option of hashtables in PHP since PHP does not have native arrays in it.
If you mean by recordset fetching with row versus assoc, use assoc since it doesn't contain any substantial overhead to it and it makes it obvious which you are accessing.
Fou-Lu is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 02:08 PM.


Advertisement
Log in to turn off these ads.