Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 277
  • Last Modified:

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
0
sany101
Asked:
sany101
  • 3
  • 2
1 Solution
 
Roger BaklundCommented:
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

0
 
sany101Author Commented:
Perfect thanks cxr,

How can I also add the file size?

Sany



0
 
Roger BaklundCommented:
Sorry, forgot the size. This should do it:
$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];
  $size = $stats[7];
  $found[$modtime] = array($entry,$size);
}
ksort($found);  # sort by key
echo 'count:'.count($found);
echo '<table>';
foreach($found as $modtime => $filedata) {
  list($filename,$size) = $filedata;
  echo '<tr>'.
       '<td>'.$filename.'</td>'.
       '<td align="right">'.$size.'</td>'.
       '<td>'.date('m/d-Y H:i:s',$modtime).'</td>'.
       '</tr>';
}
echo '</table>';

Open in new window

0
 
Roger BaklundCommented:
remove line 13
0
 
sany101Author Commented:
Thanks
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now