How can PHP be used to read and output .txt files with unknown filenames in a specific directory?
To read and output .txt files with unknown filenames in a specific directory using PHP, you can use the glob() function to retrieve an array of all .txt files in the directory. Then, you can loop through each file in the array and use file_get_contents() to read the contents of each file and echo it out.
$directory = "path/to/directory/";
$files = glob($directory . "*.txt");
foreach ($files as $file) {
$content = file_get_contents($file);
echo $content;
}
Keywords
Related Questions
- What are the best practices for storing and retrieving language preferences from a database in PHP?
- What are the potential pitfalls of using PHP to calculate capacity, as seen in the example provided?
- What are the best practices for handling image uploads and updates in PHP to prevent overwriting existing images or incorrect updates?