Last active
August 29, 2015 14:06
-
-
Save oliverbooth/028e363abd7dba52a264 to your computer and use it in GitHub Desktop.
Randomly choose a file
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| /** | |
| * Randomly chooses a file from the given directory. | |
| * @param directory Optional. The directory to search. Defaults to the current directory. | |
| * @param ext Optional. An array of accepted file extensions to match. Defaults to standard images. | |
| * @return A string representing the name of the file | |
| */ | |
| function randomFile($directory = ".", $ext = array("jpg", "jpeg", "gif", "png", "bmp")) { | |
| // Find file matches using glob | |
| $files = glob($directory."/*.{".implode(",",$ext)."}", GLOB_BRACE); | |
| // Randomly choose a file | |
| $rand = mt_rand(0, count($files) - 1); | |
| $file = $files[$rand]; | |
| return $file; | |
| } | |
| ?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment