andz Posted October 17, 2008 Share Posted October 17, 2008 I have a folder called "samples" which contains the following text files array("test1.txt", "test2.txt", test3.txt"); I know a code to open a certain file, browse and edit its content. I need a code or is it possible for me to load random text files 1 at a time based on the text files above from the samples folder??? It's like mapping the whole directory and randomly open different text file every time the page reloads no matter how many files were there on the samples folder. Link to comment https://forums.phpfreaks.com/topic/128796-solved-load-text-files-from-a-folder/ Share on other sites More sharing options...
ratcateme Posted October 17, 2008 Share Posted October 17, 2008 $files = shuffle(glob("*.txt")); Should do it. Scott. Link to comment https://forums.phpfreaks.com/topic/128796-solved-load-text-files-from-a-folder/#findComment-667730 Share on other sites More sharing options...
ghostdog74 Posted October 17, 2008 Share Posted October 17, 2008 you can shuffle the array first srand((float)microtime() * 1000000); $array = array("test1.txt", "test2.txt", "test3.txt"); shuffle($array); foreach ($array as $file { echo "$file\n "; } Link to comment https://forums.phpfreaks.com/topic/128796-solved-load-text-files-from-a-folder/#findComment-667731 Share on other sites More sharing options...
andz Posted October 17, 2008 Author Share Posted October 17, 2008 $files = shuffle(glob("*.txt")); Should do it. Scott. Could you create a full sample out of this code? I just used this to my existing code. $file = shuffle(glob('SAMPLES/*.txt')); $fileOpen = fopen($file, 'r'); $content = fread($fileOpen, filesize($file)); echo $content; I only get the value of 1 as an output. Link to comment https://forums.phpfreaks.com/topic/128796-solved-load-text-files-from-a-folder/#findComment-667743 Share on other sites More sharing options...
ghostdog74 Posted October 17, 2008 Share Posted October 17, 2008 shuffle function shuffles the array in place. the return value is true or false. Use it like i have posted. Always check the PHP manual for reference. Link to comment https://forums.phpfreaks.com/topic/128796-solved-load-text-files-from-a-folder/#findComment-667769 Share on other sites More sharing options...
ratcateme Posted October 17, 2008 Share Posted October 17, 2008 try this $files = shuffle(glob('SAMPLES/*.txt')); foreach($files as $filename ){ $fileOpen = fopen($filename , 'r'); $content = fread($fileOpen, filesize($filename )); echo "Content of:".$filename ."<br>".$content; } glob() will return an array of all files matting the pattern. shuffle() will randomize the array. Scott. Link to comment https://forums.phpfreaks.com/topic/128796-solved-load-text-files-from-a-folder/#findComment-667781 Share on other sites More sharing options...
andz Posted October 17, 2008 Author Share Posted October 17, 2008 you can shuffle the array first srand((float)microtime() * 1000000); $array = array("test1.txt", "test2.txt", "test3.txt"); shuffle($array); foreach ($array as $file { echo "$file\n "; } I've written a code out of those suggested above including yours. Please look below <?php srand((float)microtime() * 1000000); $files = glob("adverts/wide/*.txt"); shuffle($files); foreach ($files as $file) { // file name echo $file . '<br />'; $filename = $file; $handle = fopen($filename, 'r'); $contents = fread($handle, filesize($filename)); // output the contents here echo $contents; fclose($handle); } ?> Is there a way to LIMIT that only 1 text file will be browse? Thank you. Link to comment https://forums.phpfreaks.com/topic/128796-solved-load-text-files-from-a-folder/#findComment-667785 Share on other sites More sharing options...
ghostdog74 Posted October 17, 2008 Share Posted October 17, 2008 You can just grab for the file at a specific index, eg the first item. How do you get the first item? $array[0]. check the PHP manual for more info. Link to comment https://forums.phpfreaks.com/topic/128796-solved-load-text-files-from-a-folder/#findComment-667786 Share on other sites More sharing options...
ratcateme Posted October 17, 2008 Share Posted October 17, 2008 change foreach ($files as $file) { to $file = $files[0]; Scott. Link to comment https://forums.phpfreaks.com/topic/128796-solved-load-text-files-from-a-folder/#findComment-667787 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.