What are some methods in PHP to randomly select a filename from a directory and write it to a TXT file?
To randomly select a filename from a directory and write it to a TXT file in PHP, you can use the `scandir()` function to get an array of all filenames in the directory, then use `array_rand()` to select a random filename from the array. Finally, you can write this selected filename to a TXT file using `file_put_contents()`.
$dir = 'path/to/directory';
$files = scandir($dir);
$randomFile = $files[array_rand($files)];
file_put_contents('random_filename.txt', $randomFile);
Keywords
Related Questions
- What are the potential pitfalls of using SQL LIKE function in PHP for searching database records?
- What is the significance of properly handling MySQL queries in PHP to avoid syntax errors?
- How can beginners in PHP programming effectively navigate and utilize online documentation, such as the MySQL Manual, to troubleshoot coding issues?