How can PHP be used to read the contents of a specific directory and allow users to open files within it while restricting access to parent directories?
To read the contents of a specific directory in PHP and allow users to open files within it while restricting access to parent directories, you can use the `scandir()` function to get the list of files in the directory and then use a conditional statement to check if the requested file is within the specified directory. You can also use `basename()` function to ensure that only the filename is used to prevent access to parent directories.
$directory = 'specific_directory_path/';
$files = scandir($directory);
if(isset($_GET['file']) && in_array($_GET['file'], $files)) {
$file = basename($_GET['file']);
$file_path = $directory . $file;
// Code to open and display the file
} else {
echo 'File not found.';
}