Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 18th, 2012  |  syntax: None  |  size: 2.64 KB  |  hits: 13  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. A minor issue while iteration of array in php.Guidance please
  2. <?php
  3. if ($handle = opendir("/home/work/collections/utils/")) {
  4.  
  5.     while (false !== ($file = readdir($handle))) {
  6.         if ($file == '.' || $file == '..') {
  7.             continue;
  8.         }
  9.         $actual_file=pathinfo("/etrade/home/collections/utils");
  10.         if (($actual_file["extension"]== "txt") ||
  11.             ($actual_file["extension"]== "jpg") ||      
  12.             ($actual_file["extension"]== "pdf")) {
  13.             //Require changes here.Dont know how to iterate and get the list of files
  14.             echo "<td>"."n"." $actual_file['basename']."</a></td>";        
  15.         }
  16.     }
  17.  
  18.     closedir($handle);
  19. }
  20.        
  21. <?php
  22.  
  23.   $ignoreFiles = array('.','..'); // Items to ignore in the directory
  24.   $allowedExtensions = array('txt','jpg','pdf'); // File extensions to display
  25.  
  26.   $files = array();
  27.   $max = 0;
  28.  
  29.   if ($handle = opendir("/home/work/collections/utils/")) {
  30.     while (false !== ($file = readdir($handle))) {
  31.       if (in_array($file, $ignoreFiles)) {
  32.         continue; // Skip items to ignore
  33.       }
  34.       // A simple(ish) way of getting a files extension
  35.       $extension = strtolower(array_pop($exploded = explode('.',$file)));
  36.       if (in_array($extension, $allowedExtensions)) { // Check if file extension is in allow list
  37.         $files[$extension][] = $file; // Create an array of each file type
  38.         if (count($files[$extension]) > $max) $max = count($files[$extension]); // Store the maximum column length
  39.       }
  40.     }
  41.     closedir($handle);
  42.   }
  43.  
  44.   // Start the table
  45.   echo "<table>n";
  46.  
  47.   // Column headers
  48.   echo "  <tr>n";
  49.   foreach ($files as $extension => $data) {
  50.     echo "    <th>$extension</th>n";
  51.   }
  52.   echo "  </tr>n";
  53.  
  54.   // Table data
  55.   for ($i = 0; $i < $max; $i++) {
  56.     echo "  <tr>n";
  57.     foreach ($files as $data) {
  58.       if (isset($data[$i])) {
  59.         echo "    <td>$data[$i]</td>n";
  60.       } else {
  61.         echo "    <td />n";
  62.       }
  63.     }
  64.     echo "  </tr>n";
  65.   }
  66.  
  67.   // End the table
  68.   echo "</table>";
  69.        
  70. <?php
  71. if ($handle = opendir("/home/work/collections/utils/")) {
  72.     while (false !== ($file = readdir($handle))) {
  73.         if ($file == '.' || $file == '..') {
  74.             continue;
  75.         }
  76.         $actual_file=pathinfo($file);
  77.  
  78.         switch ($actual_file['extension'])
  79.         {
  80.             case ('jpg'):
  81.                 $jpegfiles[] = $actual_file;
  82.                 break;
  83.  
  84.             case ('pdf'):
  85.                 $pdffiles[] = $actual_file;
  86.                 break;
  87.         }
  88.     }
  89.     closedir($handle);
  90. }
  91.  
  92.  
  93. echo "JPG files:"
  94. foreach($jpegfiles as $file)
  95. {
  96.   echo $file['basename'];
  97. }
  98.  
  99. echo "PDF Files:"
  100. foreach($pdffiles as $file)
  101. {
  102.   echo $file['basename'];
  103. }
  104. ?>