0

I have several files in a directory.I want to display all those filenames with the extension .txt and .jpeg

<?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);
}

Please help me on how to iterate and get the list of files .For instance I want all files with jpg extension in a seperate column and pdf files in a seperate column(since I am going to display in a table)

3
  • What's the actual problem you're having. Won't that sample echo out all the file names? Commented Dec 1, 2011 at 10:43
  • there's a double quote that doesn't belong there. echo "<td>"."\n"." $actual_file['basename']."</a></td>"; should be echo "<td>"."\n". $actual_file['basename']."</a></td>";. Is this also in your code? Commented Dec 1, 2011 at 10:49
  • @peter No.It displays a array.I want all jpg files in a separate column all sqld in seperate column so on Commented Dec 1, 2011 at 10:49

2 Answers 2

1

See if this does what you want (EDITED):

<?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>";
Sign up to request clarification or add additional context in comments.

4 Comments

@peter:I want a table containing three column.First is the serial no.Second is the list of file that has the extension txt.This is the list of files that has the extension jpg(since there are son manu files.Hope I am clear
peter? Who's peter? and where are you getting the serial no from? and what is the third column?
@peter SNO is something to be added which is dynamic.Base on the no of files S.No gets its no.
@user472625 Please can you edit your question with an example list of files, and an example of the HTML you would like to generate based on that list?
0

If you just want to display two lists of files (it's not clear what part you're having trouble with from your question) can't you just store the filenames in an array?

You don't seem to be getting the file details - you're getting the pathinfo for /etrade/home/collections/utils, but you never add the file name to it.

<?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'];
}
?>

Obviously you can be cleverer with the arrays, and have use multi-dimensional arrays and do away with the switch if you want.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.