What potential issue could arise from listing files in a directory using PHP?
One potential issue that could arise from listing files in a directory using PHP is exposing sensitive information, such as configuration files or user data, to unauthorized users. To solve this issue, you can restrict the files that are displayed by checking the file extension or using an access control mechanism.
$directory = 'path/to/directory';
$allowedExtensions = ['jpg', 'png', 'pdf']; // Define allowed file extensions
$files = scandir($directory);
foreach ($files as $file) {
$fileInfo = pathinfo($file);
if (in_array($fileInfo['extension'], $allowedExtensions)) {
echo $file . "<br>";
}
}
Keywords
Related Questions
- What potential pitfalls should be considered when concatenating strings in PHP arrays?
- What are some best practices for defining classes in PHP when creating modules like guestbooks and messaging systems?
- What is the recommended way to view the output of a PHP file without constantly refreshing in a browser?