php" is_dir" Intranet站点的文件列表

时间:2014-03-24 21:44:54

标签: php directory intranet

我正在创建一个测试文件,以获取位于Intranet文件夹中的所有文件,并根据其扩展名简单地在列出的文件旁边放置一个图标图像。 出于某种原因,代码没有正确地提取内容。

以下是目录中文件的Windows资源管理器视图的屏幕截图。

enter image description here

这是我的代码:

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

?>

这就是我运行时的样子:

enter image description here

问题是未列出的某些扩展程序正在拉动“Microsoft WORD”图标。

扩展名为“.mdb”的“Access Database”文件正在拉动一个Word图标,扩展名“.mdb_hcu”也会拉出一个WORD图标。

如何使其成为BLANK文件选项(请参阅我的开关案例,其中答案为NULL)

另外,如何组织以便首先列出文件夹,并且所有内容都按字母顺序排列?

是否有插件可以让我更轻松地实现这一目标?

2 个答案:

答案 0 :(得分:2)

您可以将case NULL:更改为default:,看看它是否有效?

default:表示如果它不匹配任何&#34;案例&#34;然后它将运行默认值。

由于

:编辑答案:

您需要更改设置$img = 'folder_img.jpg'的位置,因为它将其设置为单词,然后在下一个“&#39;”时,它不是dir,所以不会改变,但也不匹配任何情况,所以保持原来的while - 希望这是有道理的!

让我知道它是否适合你!

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

答案 1 :(得分:2)

问题是你不能列出这样的案例值:

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

这将被评估为一个将评估为true的语句。然后,扩展名将与该结果进行比较,除非扩展名为NULL,否则案例将为真。

您需要列出每个案例选项,如下所示:

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

如果任何一种情况属实(等于扩展名),则案例中的代码将一直运行,直到break被命中。