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 be used to compare dates in the format YYYY-MM-DD?
- Are there alternative PHP libraries or tools that can simplify the process of parsing and rendering BBCode content while avoiding issues with line breaks?
- What are the different methods of redirecting users in PHP, and when is each method most appropriate to use?