How can the user modify the PHP script to only display a specific type of file, such as *.jpg files?
To only display a specific type of file, such as *.jpg files, the user can modify the PHP script by adding a conditional check to filter out files that do not have the desired file extension. This can be done by using the pathinfo() function to get the file extension and then comparing it to the desired extension, in this case, ".jpg".
<?php
$dir = "path/to/directory";
$files = scandir($dir);
foreach($files as $file) {
if(is_file($dir . '/' . $file) && pathinfo($file, PATHINFO_EXTENSION) == 'jpg') {
echo $file . "<br>";
}
}
?>
Related Questions
- In what situations should placeholders be used in PHP code, and how can they be effectively managed?
- How can errors in accessing non-existent variables in PHP arrays be avoided when selecting random elements?
- What are the advantages of using bitwise operations in PHP for handling multiple-choice checkbox selections?