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
- How can PHP developers troubleshoot and resolve browser compatibility issues related to PHP code execution?
- What are the potential pitfalls of using substr() to manipulate strings in PHP, as seen in the forum thread?
- What potential issues can arise when using the file() function to check for a newline character at the end of a file?