Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9
  1. #1
    New Coder
    Join Date
    May 2005
    Location
    Utah
    Posts
    65
    Thanks
    6
    Thanked 0 Times in 0 Posts

    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!

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    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.

  • #3
    New Coder
    Join Date
    May 2005
    Location
    Utah
    Posts
    65
    Thanks
    6
    Thanked 0 Times in 0 Posts
    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); 

  • #4
    Regular Coder
    Join Date
    Dec 2009
    Posts
    173
    Thanks
    24
    Thanked 1 Time in 1 Post
    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!

  • #5
    New Coder
    Join Date
    May 2005
    Location
    Utah
    Posts
    65
    Thanks
    6
    Thanked 0 Times in 0 Posts
    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?

  • #6
    New Coder
    Join Date
    May 2005
    Location
    Utah
    Posts
    65
    Thanks
    6
    Thanked 0 Times in 0 Posts
    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 05:21 AM.

  • #7
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    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.

  • #8
    New Coder
    Join Date
    Oct 2012
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Regarding php

    Hi, I want to ask you that which will be more suitable for database, hash tables or arrays?

  • #9
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •