CodingForums.com

CodingForums.com (http://www.codingforums.com/index.php)
-   PHP (http://www.codingforums.com/forumdisplay.php?f=6)
-   -   Need Array to sort files (http://www.codingforums.com/showthread.php?t=284551)

Local Hero 12-22-2012 09:03 AM

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!

Fou-Lu 12-22-2012 03:24 PM

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.

Local Hero 12-24-2012 06:11 AM

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


sorlaker 12-24-2012 01:01 PM

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!

Local Hero 12-25-2012 06:51 PM

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 12-25-2012 07:30 PM

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.

Fou-Lu 12-26-2012 03:45 PM

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.

paulinetaylor85 12-27-2012 10:42 AM

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

Fou-Lu 12-27-2012 02:28 PM

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.


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

Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.