Search

PHP Script to Generate a Link Directory from HTML files

0 views

Here's an example of a

Prompt
<!DOCTYPE html> <html> <head> <title>Link Directory</title> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css"> <script src="https://code.jquery.com/jquery-3.3.1.min.js"></script> <script src="https://cdn.datatables.net/1.10.24/js/jquery.dataTables.min.js"></script> <link rel="stylesheet" href="https://cdn.datatables.net/1.10.24/css/jquery.dataTables.min.css"> </head> <body> <div class="container"> <h1>Link Directory</h1> <table id="linkTable" class="table"> <thead> <tr> <th>File Name</th> <th>Creation Date</th> </tr> </thead> <tbody> <?php $directory = './'; // Path to the directory to scan $fileExtensions = ['html']; // File extensions to include // Recursive function to scan directory and subdirectories function scanDirectory($dir) { global $fileExtensions; $htmlFiles = []; $files = scandir($dir); foreach ($files as $file) { if ($file === '.' || $file === '..') { continue; } $filePath = $dir . '/' . $file; if (is_dir($filePath)) { $htmlFiles = array_merge($htmlFiles, scanDirectory($filePath)); } else { $fileExtension = pathinfo($file, PATHINFO_EXTENSION); if (in_array($fileExtension, $fileExtensions)) { $htmlFiles[] = $filePath; } } } return $htmlFiles; } $htmlFiles = scanDirectory($directory); // Sort files by creation date usort($htmlFiles, function($a, $b) { return filemtime($a) - filemtime($b); }); foreach ($htmlFiles as $file) { $fileName = basename($file); $creationDate = date('Y-m-d H:i:s', filemtime($file)); $relativePath = ltrim(substr($file, strlen($directory)), '/'); echo '<tr>'; echo '<td><a href="' . $relativePath . '">' . $fileName . '</a></td>'; echo '<td>' . $creationDate . '</td>'; echo '</tr>'; } ?> </tbody> </table> </div> <script> $(document).ready(function() { $('#linkTable').DataTable(); }); </script> </body> </html>

Make sure to save this script with a .php extension (e.g., link_directory.php) and place it in the directory where you want to generate the link directory web page. When you open the script in a web browser, it will display a table with links to all .html files in the directory and subdirectories, sorted by creation date. The table is sortable using the DataTables plugin provided by Ajax.

Share this article

Comments (0)

Please sign in to leave a comment.

No comments yet. Be the first to comment!