代码之家  ›  专栏  ›  技术社区  ›  bagofmilk

用于Intranet站点的php“is_dir”文件列表

  •  1
  • bagofmilk  · 技术社区  · 12 年前

    我正在创建一个测试文件,以获取位于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 回复  |  直到 12 年前
        1
  •  2
  •   Jonathan Kuhn Reignier Julien    12 年前

    问题是您不能这样列出大小写值:

    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 被击中。

        2
  •  2
  •   Gary Harrower    12 年前

    你能改变吗 case NULL: default: 看看能不能用?

    违约: 意味着如果它不匹配任何“case”,那么它将运行默认的case。

    谢谢

    :已编辑答案:

    你需要改变你设定的位置 $img = 'folder_img.jpg' 当它将其设置为word时,在下一个'while'时,它不是一个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);
        }
    ?>