• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 280
  • 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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

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