سؤال

I'm creating a test file to get all files located in an intranet folder and simply put an icon image next to the file listed according to its extension. For some reason, the code is not pulling things correctly.

Here's a screen shot of the windows explorer view of the files in the directory.

enter image description here

And here is my code:

<?php

  $dir="file:///.....to_my_directory"; // Directory where files are stored

  if ($dir_list = opendir($dir)) {
     while(($filename = readdir($dir_list)) != false) {

        if (is_dir($filename)) {
      $img = 'folder_img.jpg';
    } else {

      $filestuff = pathinfo($filename);
      $file_ext = isset($filestuff['extension']) ? $filestuff['extension'] : null;
      switch($file_ext) {
        case 'pdf':
        $img = 'pdf_img.jpg';
        break;

        case 'doc' || 'docx' || 'docm' || 'dotm' || 'dotx':
        $img = 'word_img.jpg';
        break;

        case 'xls' || 'xlsm' || 'xltx' || 'xlam' || 'xlsx' || 'xlm' || 'xlt' || 'xlsb':
        $img = 'excel_img.jpg';
        break;

        case 'txt':
        $img = 'txt_file_img.jpg';
        break;

        case NULL:
        $img = 'blank_file_img.jpg';
        break;
      }
   }



?>
<p><span><img src='<?php echo $img; ?>' height='23px;'/><a href="<?php echo $filename; ?>"><?php echo $filename;?></a></span></p>


<?php
  }
    closedir($dir_list);
  }

?>

AND this is how it looks when I run it:

enter image description here

The problem is that certain extensions not listed are pulling the 'Microsoft WORD' icon.

The 'Access Database' file with extension '.mdb' is pulling a Word icon, the extension '.mdb_hcu' is also pulling a WORD icon.

How do I make it the BLANK file option (see my switch case where answer is NULL)

Also, how do I organize so that the folders are listed first and everything is in alphabetical order?

Is there a plugin that allows me to achieve this easier?

هل كانت مفيدة؟

المحلول

Problem is that you can't list case values like this:

case 'doc' || 'docx' || 'docm' || 'dotm' || 'dotx':

That will get evaluated as one statement which will evaluate to true. Then the extension will compare to that result and unless the extension is NULL, the case will be true.

You need to list out each case option like so:

case 'doc':    //if
case 'docx':   //any
case 'docm':   //of
case 'dotm':   //these
case 'dotx':   //are true
               //this will run
    //set image
    break;     //up to this

If any of the cases are true (equal to the extension), the code inside the case will run until the break is hit.

نصائح أخرى

Can you change case NULL: to default: and see if it works please?

default: means that if it doesn't match any of the "cases" then it will run the default one.

Thanks

:Edited Answer:

You need to change where you set $img = 'folder_img.jpg' as it sets it to word, then on the next 'while', it's not a dir, so doesn't change, but also doesn't match any case, so stays as it was on the last while - hope this makes sense!

Let me know if it works for you!

<?php

    $dir="."; // Directory where files are stored

    if ($dir_list = opendir($dir)) {
        while(($filename = readdir($dir_list)) != false) {
            $img = 'folder_img.jpg';
            if (!is_dir($filename)) {
                $filestuff = pathinfo($filename);
                $file_ext = isset($filestuff['extension']) ? $filestuff['extension'] : null;
                switch($file_ext) {
                    case 'pdf':
                        $img = 'pdf_img.jpg';
                    break;

                    case 'doc':
                    case 'docx':
                    case 'docm':
                    case 'dotm':
                    case  'dotx':
                        $img = 'word_img.jpg';
                    break;

                    case 'xls':
                    case 'xlsm':
                    case 'xltx':
                    case 'xlam':
                    case 'xlsx':
                    case 'xlm':
                    case 'xlt':
                    case 'xlsb':
                        $img = 'excel_img.jpg';
                    break;

                    case 'txt':
                        $img = 'txt_file_img.jpg';
                    break;

                    default:
                        $img = 'blank_file_img.jpg';
                    break;
                }
            }
?>
<p><span><img src='<?php echo $img; ?>' height='23px;'/><a href="<?php echo $filename; ?>"><?php echo $filename;?></a></span></p>


<?php
        }
    closedir($dir_list);
    }
?>
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top