I have been trying to read a directory, write it to a file then read the file into an array and shuffle it before I write it to the file. There is probably a one step method, but having a mind block.
I want to get the contents of a directory into a random listing into a file.
The problem is (in my case) there are 5 files in the directory when I read it and shuffle it I only get four. The last item in the file is not included. When I refresh the screen the missing one comes in as a blank line.
I have included the following code. if any one knows what is wrong here , your advice would be appreciated.
*******read directory and create file
if ($handle = opendir($sPath)) {
$myfile = fopen ("display.ls", "w+");
/ This is the correct way to loop over the directory. /
while (false !== ($file = readdir($handle))) { if ($file != "." && $file != ".." &&
ereg(".jpg$",$file) ) {
$fp = fwrite($myfile, $list);
}
}
close($myfile);
********** Read file into array and shuffle
$filedata = file("./display.ls");
$random = shuffle($filedata);
foreach($frandom as $file_line)
{
echo $file_line ;
}
}