簡體   English   中英

如何使用 PHP 查看文件夾內的文件

[英]How can I view files inside folder using PHP

我有一個從目錄中讀取的文件列表,每個文件/文件夾都有一個按鈕可以在模式中查看它。 如何將單擊的確切文件傳遞給模態顯示? 這是我的代碼:

<?php
                $main_dir = "directories/{$user->email}";
                $active_row;
                chdir($main_dir);
                $dh = opendir('.');
                while ($file = readdir($dh)) {
                    if ($file != "." && $file != "..") { ?>
                        <tr>
                            <td class="file-name">
                                <?php
                                if (filetype($file) === 'dir') { ?>
                                    <a href="<?php echo $main_dir . "/" . $file; ?>"><i class="far fa-folder"></i><?php echo $file ?></a>
                                <?php
                                } else {
                                    echo '<span>' . $file . '</span>';
                                    echo '<em>' . $file . '</em>';
                                }
                                ?>
                            </td>
                            <td class="extension">
                                <?php
                                $path = pathinfo($file);
                                if (filetype($file) === 'dir') {
                                    echo 'folder';
                                } else {
                                    echo $path['extension'];
                                }
                                ?>
                            </td>
                            <td class="date">
                                <?php echo date("F d Y H:i:s.", filectime($file)) ?>
                            </td>
                            <td class="manage">
                                <span class="view" <?php if (filetype($file) !== 'dir') {
                                                        echo 'id="' . $file . '"';
                                                    }
                                                    ?> data-toggle="modal" data-target="#view-file-modal"><i class="fas fa-eye"></i></span>
                                <form action="deleteFile.php" method="POST">
                                    <input type="hidden" name="fileName" value="<?php echo $file ?>">
                                    <button type="submit" name="deleteFile" class="delete"><i class="far fa-trash-alt"></i></button>
                                </form>
                            </td>
                        </tr>
                <?php
                    }
                }
                closedir($dh);
                ?>

這是我使用引導程序的模式:

<!-- View File Modal -->
            <div class="modal fade" id="view-file-modal" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
                <div class="modal-dialog">
                    <div class="modal-content">
                        <div class="modal-body">
                            <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                                <span aria-hidden="true">&times;</span>
                            </button>
                            <img src="" alt="image">
                        </div>
                    </div>
                </div>
            </div>

這張圖片顯示了我所擁有的: 在此處輸入圖像描述

最后,我知道我在使用的查看方法上犯了一個錯誤,但它僅適用於使用 jQuery 的圖像

不要使用id=""使用數據屬性data-file=""它不會干擾樣式等(想象一個名為view-file-modal ,它會破壞模式),

然后使用模態事件獲取data-file的值,該值將在e.relatedTarget.dataset.file

 $('#myModal').on('show.bs.modal', function(e) { let file = e.relatedTarget.dataset.file || '' console.log(file) $(this).find('img[data-img]').attr('src', file) })
 <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous"> <script src="https://code.jquery.com/jquery-3.4.1.slim.min.js" integrity="sha384-J6qa4849blE2+poT4WnyKhv5vZF5SrPo0iEjwBvKU7imGFAV0wwj1yYfoRSJoZ+n" crossorigin="anonymous"></script> <script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.0/dist/umd/popper.min.js" integrity="sha384-Q6E9RHvbIyZFJoft+2mJbHaEWldlvI9IOYy5n3zV9zzTtmI3UksdQRVvoxMfooAo" crossorigin="anonymous"></script> <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js" integrity="sha384-wfSDF2E50Y2D1uUdj0O3uMBJnjuUD4Ih7YwaYd1iqfktj0Uod8GCExl3Og8ifwB6" crossorigin="anonymous"></script> <button type="button" id="mymodal" class="btn btn-link" data-toggle="modal" data-target="#myModal" data-file="https://i.imgur.com/RRWNchB.jpg"> Launch demo modal </button> <;-- Modal --> <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel"> <div class="modal-dialog" role="document"> <div class="modal-content"> <div class="modal-header"> <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times:</span></button> </div> <div class="modal-body"> <img class="img-fluid" data-img src="data;image/png,base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mP89h8AAvEB93wyFi8AAAAASUVORK5CYII=" alt> </div> <div class="modal-footer"> <button type="button" class="btn btn-default" data-dismiss="modal">Close</button> <button type="button" class="btn btn-primary">Save changes</button> </div> </div> </div> </div>

暫無
暫無

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

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