简体   繁体   English

如何使用 PHP 查看文件夹内的文件

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

I have a list of files read from a directory, each file/folder has a button to view it inside modal.我有一个从目录中读取的文件列表,每个文件/文件夹都有一个按钮可以在模式中查看它。 How can I pass exact file clicked to modal to show?如何将单击的确切文件传递给模态显示? Here is my code:这是我的代码:

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

and this is the modal I have using bootstrap:这是我使用引导程序的模式:

<!-- 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>

and This image show what I have exactly:这张图片显示了我所拥有的: 在此处输入图像描述

Finally, I know I made a mistake on viewing method I use, but It working only on images using jQuery最后,我知道我在使用的查看方法上犯了一个错误,但它仅适用于使用 jQuery 的图像

Dont use id="" use a data attribute data-file="" it wont interfere with styling etc (imagine a file called view-file-modal , it would break the modal),不要使用id=""使用数据属性data-file=""它不会干扰样式等(想象一个名为view-file-modal ,它会破坏模式),

Then use the modal events to get the value of data-file which will be in e.relatedTarget.dataset.file然后使用模态事件获取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.

相关问题 如何使用PHP计算文件夹中的文件? - How to count files inside a folder using PHP? 如何使网站可读但文件下载管理器无法下载的包含/文件夹中的 php 文件? - How can I make the php files inside an includes/ folder readable by the website but not downloadable by file download managers? 如何在保留文件原始时间戳的同时使用PHP从zip文件夹中提取文件? - How can I extract files from a zip folder using PHP while retaining the files original timestamps? 我如何使用 JavaScript 上传带有密钥生成名称的文件夹内 Firebase 存储上的文件 - How can i upload files on Firebase storage inside folder with key generated names using JavaScript 如何使用PHP递归地将所有具有特定名称的文件包含在文件夹中? - How can I recursively include all files with a specific name in a folder using PHP? 如何删除文件夹中的php文件? - How can I delete a php file inside a folder? 如何保护PHP站点的文件夹中的文件? - How can I protect files in a folder in a PHP site? 如何使用PHP从Rackspace Cloud Files容器内的文件夹中获取文件夹大小? - How to get folder size from folder inside Rackspace Cloud Files container using PHP? 我如何在控制器中使用this-&gt; data []在循环中传递变量,以在PHP CI中查看 - How can I pass the variables inside loop using this->data[] from controller to view in php CI 无法使用PHP上传文件夹内的文件 - Can not Upload file inside folder using PHP
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM