Link to home
Create AccountLog in
Avatar of sany101
sany101

asked on

PHP to read dir and echo zip files as download links with size and date info per table row

Hi,

How can I do the following?

I would like to have a directory read and then each Zip file within to be echoed in a separate row by date order as a download link with file size and file date/time on the same row but in different columns.  

Thanks
Avatar of Roger Baklund
Roger Baklund
Flag of Norway image

Try something like this:
$folder = '/path/to/directory';
$dir = dir($folder);
$found = array();
while(false!==($entry=$dir->read())) {
  $path_parts = pathinfo($folder.'/'.$entry);
  if($path_parts['extension'] != 'zip') continue;
  $stats = stat($folder.'/'.$entry);
  $modtime = $stats[9];
  $found[$modtime] = $entry;
}
ksort($found);  # sort by key
echo '<table>';
foreach($found as $modtime => $filename)
  echo '<tr><td>'.$filename.'</td><td>'.
    date('m/d-Y H:i:s',$modtime).'</td></tr>';
echo '</table>';

Open in new window

Avatar of sany101
sany101

ASKER

Perfect thanks cxr,

How can I also add the file size?

Sany



ASKER CERTIFIED SOLUTION
Avatar of Roger Baklund
Roger Baklund
Flag of Norway image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
remove line 13
Avatar of sany101

ASKER

Thanks