I would love some feed back on this code. Not sure if it's the best way to do this or not. I'm reading all the files in a subdir and then displaying them. Including date, filename, and file size. Sorted by date, most recent first.

One thing I need to fix up... when the file name is clicked, the file is opened. However, if the filename has spaces, then it is not opened correctly. What can I do to handle file names with spaces?

Here's the code. If there is a better way, I'd love to hear it! 🙂

<? 
$dirpath = getcwd() . "/minutes/";
$dir = opendir($dirpath);
$ar2 = array();
$arrC = 0;
$arrC2 = 0;
$arrC3 = 0;
while ($file = readdir($dir)) {
   $localpath = $dirpath.$file;
   if (is_file($localpath)) {
       $key = filemtime($localpath).md5($file);
       $uts=filemtime($localpath); // Get the filetime for sorting
       $filedate = date ('F d, Y g:i A', $uts); // Format filetime for display
       $filebig = filesize($localpath); // Get the file size for display
       // Plop the values into an array for sorting later
       $ar2[$key][$arrC][$arrC2][$arrC3] = $filedate;
       $arrC3++;
       $ar2[$key][$arrC][$arrC2][$arrC3] = $file;
       $arrC3++;
       $ar2[$key][$arrC][$arrC2][$arrC3] = $filebig;
       $arrC++;
       $arrC2 = 0;
       $arrC3 = 0;

   }
}

?>
<table cellpadding=3 cellspacing=0 border=0 bordercolor=black align=center> 
<tr><th>Uploaded Date</th><th>Name</th><th>Size</th>
<?

// Sort the array values, most recent goes on top
$countcells = 1;
krsort($ar2);
foreach ($ar2 as $x1) {
   echo "<tr>";
   foreach ($x1 as $a2) {
   	foreach ($a2 as $y2) {
           foreach ($y2 as $x3) {
             if ($countcells == 1) { // File date
               echo "<td class=mins width=200>$x3</td>";
             }
             elseif ($countcells == 2) { // File Name
               echo "<td class=mins width=150> <a href=minutes/$x3>$x3</a></td>";
             } elseif ($countcells == 3) { // File Size
               echo "<td class=mins width=150>";
               $fsize = $x3;
               if($fsize < 1024){
                  $fsize = number_format($fsize, 1);
                  $fsize .= ' KB'; 
                  echo "$fsize";
               } else {
                  if(($fsize / 1024 / 1024) < 1024) {
                     $fsize = number_format($fsize / 1024, 1);
                     $fsize .= ' MB';
                     echo "$fsize";
                  } elseif(($fsize / 1024 / 1024 / 1024) < 1024) {
                    $fsize = number_format($fsize / 1024 / 1024, 1);
                    $fsize .= ' GB';
                    echo "$fsize";
                  } else {
                     $fsize = number_format($fsize / 1024 / 1024 / 1024, 1);
                     $fsize .= ' TB';
                     echo "$fsize";
                  } 
               }
               echo "</td>";
             } 
             $countcells++;
           }
       }
   }
   echo "</tr>";
   $countcells = 1;
}
closedir($dir);
?> 
</table> 

    That's quite a heavily nested array, and alot of foreach-ing. Is it not reasonable to just have

    $arr = array(
      "filename" => array (
        "mtime" => "12345",
        "size" => "98765"
        //etc...
      )
    );
    

    It would be easier to create and to modify.

    I would think providing some additional sorting methods would be helpful too (alphabetic, by size, by type etc).

    As for the spaces, check that the names with spaces are being properly decoded.

      Thanks for your input. I'm very new to PHP so I don't know the best way to do this. It did seem to me, also, to be very heavily nested and I didn't like that.

        Write a Reply...