- A minor issue while iteration of array in php.Guidance please
- <?php
- if ($handle = opendir("/home/work/collections/utils/")) {
- while (false !== ($file = readdir($handle))) {
- if ($file == '.' || $file == '..') {
- continue;
- }
- $actual_file=pathinfo("/etrade/home/collections/utils");
- if (($actual_file["extension"]== "txt") ||
- ($actual_file["extension"]== "jpg") ||
- ($actual_file["extension"]== "pdf")) {
- //Require changes here.Dont know how to iterate and get the list of files
- echo "<td>"."n"." $actual_file['basename']."</a></td>";
- }
- }
- closedir($handle);
- }
- <?php
- $ignoreFiles = array('.','..'); // Items to ignore in the directory
- $allowedExtensions = array('txt','jpg','pdf'); // File extensions to display
- $files = array();
- $max = 0;
- if ($handle = opendir("/home/work/collections/utils/")) {
- while (false !== ($file = readdir($handle))) {
- if (in_array($file, $ignoreFiles)) {
- continue; // Skip items to ignore
- }
- // A simple(ish) way of getting a files extension
- $extension = strtolower(array_pop($exploded = explode('.',$file)));
- if (in_array($extension, $allowedExtensions)) { // Check if file extension is in allow list
- $files[$extension][] = $file; // Create an array of each file type
- if (count($files[$extension]) > $max) $max = count($files[$extension]); // Store the maximum column length
- }
- }
- closedir($handle);
- }
- // Start the table
- echo "<table>n";
- // Column headers
- echo " <tr>n";
- foreach ($files as $extension => $data) {
- echo " <th>$extension</th>n";
- }
- echo " </tr>n";
- // Table data
- for ($i = 0; $i < $max; $i++) {
- echo " <tr>n";
- foreach ($files as $data) {
- if (isset($data[$i])) {
- echo " <td>$data[$i]</td>n";
- } else {
- echo " <td />n";
- }
- }
- echo " </tr>n";
- }
- // End the table
- echo "</table>";
- <?php
- if ($handle = opendir("/home/work/collections/utils/")) {
- while (false !== ($file = readdir($handle))) {
- if ($file == '.' || $file == '..') {
- continue;
- }
- $actual_file=pathinfo($file);
- switch ($actual_file['extension'])
- {
- case ('jpg'):
- $jpegfiles[] = $actual_file;
- break;
- case ('pdf'):
- $pdffiles[] = $actual_file;
- break;
- }
- }
- closedir($handle);
- }
- echo "JPG files:"
- foreach($jpegfiles as $file)
- {
- echo $file['basename'];
- }
- echo "PDF Files:"
- foreach($pdffiles as $file)
- {
- echo $file['basename'];
- }
- ?>