簡體   English   中英

顯示來自多個目錄的文件,這些目錄都有不同的文件名,並顯示在表PHP中

[英]Showing files from multiple directories which all have different file names and display in table PHP

我正在嘗試在3個單獨的目錄中顯示文件列表。 如果每個文件夾中的所有文件名都相同,則可以正常工作。 但是,如果第一個文件夾中的文件名不同,則其他兩個文件夾中不顯示圖像。 因此,所有文件名都必須相同,但我需要它,因此文件名是否相同並不重要。

到目前為止,這就是我所擁有的。

<?php
date_default_timezone_set('Europe/London');
$dirname = "dir1";
$dirnameTwo = "dir2";
$dirnameThree = "dir3";
?>
<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Transitional//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'>

<head>
    <meta http-equiv='refresh' content='10'>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
    <meta http-equiv='cache-control' content='max-age=0' />
    <meta http-equiv='cache-control' content='no-cache' />
    <meta http-equiv='expires' content='0' />
    <meta http-equiv='expires' content='Tue, 01 Jan 1980 1:00:00 GMT' />
    <meta http-equiv='pragma' content='no-cache' />
</head>
<html>

<body>
<style type="text/css">
    .pi-title {
        padding: 1rem;
    }
</style>
<div class="container">
    <div class="row">
        <div class="pi-title">
            <h3>Test</h3>
        </div>
        <div class="table-container col-md-12">
            <table class="table" border='1' cellpadding='5' cellspacing='0' bordercolor='#ccc'>
                <thead class="thead-dark">
                <tr>
                    <th scope="col">ID</th>
                    <th scope="col">File Name</th>
                    <th scope="col">PI 1</th>
                    <th scope="col">PI 2</th>
                    <th scope="col">PI 3</th>
                </tr>
                </thead>
                <tbody>
                <tr></tr>
                <tr>
                    <?php
                    array_multisort(array_map('filemtime', ($files = glob("*.*"))), SORT_DESC, $files);
                    $directories = implode(',', [ $dirname, $dirnameTwo, $dirnameThree]);
                    $files = glob("./{{$directories}}/*", GLOB_BRACE);
                    $i = 1;
                    foreach($files as $filename) {
                      if (file_exists($filename)) {
                            echo "</tr>";
                            echo "<td><font face='Arial' size='6'>$i</font></td>";
                            echo "<td><font face='Arial' size='6' color='red'>" . date ("F d Y H:i", filemtime($filename));
                            echo "</font></td>";
                        }
                        print("

                           <td><img src='$filename' height='180' width='220'></td>
                           <td><img src='$filename' height='180' width='220'></td>
                           <td><img src='$filename' height='180' width='220'></td>

                           ");
                        $i++;
                        if($i==13) break;
                    }
                    ?>
                </tr>
                </tbody>
            </table>
        </div>
    </div>
</div>

您可以使用GLOB_BRACE標志:

$directories = implode(',', [ $dirname, $dirnameTwo, $dirnameThree]);
$files = glob("./{{$directories}}/*", GLOB_BRACE);

glob('{dir1,dir2}/*', GLOB_BRACE)為您提供兩個文件夾dir1dir2中所有文件的列表。 在字符串插值中,變量周圍的花括號具有特殊含義,因此您需要雙括號,從而在插值后導致單括號。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM