What are some potential pitfalls when trying to display the folder structure of a website using PHP?

One potential pitfall when trying to display the folder structure of a website using PHP is exposing sensitive information such as directory paths or file names that could be exploited by malicious users. To prevent this, it is important to sanitize and validate the input before displaying it to the user. Another pitfall is displaying a large number of folders or files at once, which can overwhelm the user. To address this, consider implementing pagination or filtering options to improve the user experience.

<?php
// Example code to display folder structure with pagination
$directory = "path/to/website/folder";

$files = scandir($directory);
$files = array_diff($files, array('.', '..')); // Remove '.' and '..' entries

$perPage = 10;
$page = isset($_GET['page']) ? $_GET['page'] : 1;
$totalPages = ceil(count($files) / $perPage);

$offset = ($page - 1) * $perPage;
$files = array_slice($files, $offset, $perPage);

foreach ($files as $file) {
    echo $file . "<br>";
}

// Pagination links
for ($i = 1; $i <= $totalPages; $i++) {
    echo "<a href='?page=$i'>$i</a> ";
}
?>