How can you create links to documents or images based on file names retrieved using the glob() function in PHP?
When using the glob() function in PHP to retrieve file names, you can create links to these documents or images by iterating through the array of file names and generating HTML anchor tags with the file names as the href attribute. This allows users to easily access the files by clicking on the links.
$files = glob('path/to/files/*'); // Retrieve file names using glob()
foreach ($files as $file) {
$fileName = basename($file); // Get the base name of the file
echo "<a href='$file'>$fileName</a><br>"; // Create a link to the file
}
Keywords
Related Questions
- What are the advantages and disadvantages of using while loops versus functions for reading and displaying data from a MySQL database in PHP?
- What are the potential pitfalls of using Windows path format on a Linux server in PHP scripts, as highlighted in the thread?
- What are some common mistakes to avoid when working with time values in PHP functions like date() and mktime()?