Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

sort an array using timestamp values...

Status
Not open for further replies.

commun1

Programmer
May 26, 2004
41
DE
I'd like to sort an array which consists of two entries, a filename (instead of the default increasing key number) and a timestamp of when the file has been created.

my script looks like this:

Code:
while ($file = readdir ($dir)) { 
    $timestamp = filemtime($file);
    $files[$timestamp] = $file;
}

however the timestamp-value is not applied, instead I get the regular key entries. print_r($files) gives me the following:

Code:
Array
(
  [0] => filename0.txt
  [1] => filename1.txt
  [2] => filename2.txt
  [3] => filename3.txt

)

but I want to have this:

Code:
Array
(
  [1111674409] => filename0.txt
  [1106323078] => filename1.txt
  [1110561128] => filename2.txt
  [1107435271] => filename3.txt

)

without doing an multi-dimesional array.
afterwards I'd like it to sort the keys according to its timestamp. suppose I'd net sort($files) then?

thanks in advance.
 
hi,


what is your operating system?
do you have read access on the files?

pls, do something like:
Code:
while ($file = readdir ($dir)) {
    $timestamp = filemtime($file);
    print($timestamp.' '.$file.'<BR>);
    $files[$timestamp] = $file;
}

which is the output ?



___
____
 
Hi,

OS is Linux and I do have access on the files.

why do you ask?

the scripts work fine except for the fact I don't get the timestamp info into my array because I'm not aware of the correct syntax, that's all I'm asking for ;-)
 
in some cases the filemtime() could return FALSE.

you have rigth to read the files, but the `http' user ?!
(or the user which runs the apache webserver)



___
____
 
filemtime() returns the timestamp values like it should.

I'm on shared hosting. I created the files using php's fopen()-function, so that's why I have access to 'em.

SAFE_MODE = OFF
 
Have you considered the fact that you need a file system path?
Your directory list routine returns the filename. If you just check the time on the filename it will fail, since no path is prepended.
I am confident that when you prepend the path the timestamps will read correctly.
 
commun1:
Bumping threads is considered rude in Tek-Tips.


The code you're pasting...are you copying and pasting the code you're running, or are you retyping it? Is the code snippet you've posted part of a larger script, or is it the meat of the code?

This version of your code:
Code:
<?php
$dir = opendir ('.');

while ($file = readdir ($dir))
{ 
	$timestamp = filemtime($file);
	$files[$timestamp] = $file;
}

print '<html><body><pre>';
print_r($files);
print '</pre></body></html>';
?>

works on my LAMP box. I'm wondering if you have a typo or if some other part of a larger script is messing with the value of $timestamp.


Want the best answers? Ask the best questions!

TANSTAAFL!!
 
first of all sorry for bumping, will not happen again =)

@DRJ478

the path has been defined in $dir at the beginning of the script which is not shown here.

@sleipnir214

it's just the main part and I copy&pasted it.

the sniplet works fine here too (also LAMP combo), just that it doesn't assign each timestamp value to each filename, so I'd get this when printing the array:

Code:
Array
(
  [1111674409] => filename0.txt
)

maybe my explanation is simply lacking clarity...
 
If the snippet does not perform a required function, then I'd say the snippet is not working fine.

Does the script I posted associate the timestamps with the filenames on your system?


Want the best answers? Ask the best questions!

TANSTAAFL!!
 
>Does the script I posted associate the timestamps with the filenames on your system?

that's what I'm asking for =)
how can I associate them?


 
yupp!

I was using an rsort($files) afterwards and print_r($files) after that, that was my fault, stupid me ... sometimes I'm just looking in the wrong places...

thanks for your help though, without it I wouldn't have seen the error...

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top