Jump to content

Can anyone decipher this please?


real_illusions

Recommended Posts

I have a beginning of a script to generate multiple random images from a folder, but I cant work out how this bit is working:And it simply echos "random". There is nothing before this that has anything to do with this script. I've left out the rest of the script.

function RandomFile($folder='', $extensions='.*'){   // fix path:	$folder = trim($folder);	$folder = ($folder == '') ? './' : $folder;	// I've added these 2 lines to see what it echos	echo $folder;	exit();

Link to comment
Share on other sites

This is where the code is from:http://randaclay.com/random_images.txtBut its not very well explained. Found out the RandomFile("random"); is where the random is coming from, but changing that to a variable, to get the latest updated folder, caused a never ending script and almost causing the browser to crash.Kinda given up on that script. Is there an easier to way get 4 or 5 random images from a folder?

Link to comment
Share on other sites

Thanks, but I'm not getting any results.

$rnd_image_names = array_rand(glob("path/to/$variablefoldername"), 4);$result = count($rnd_image_names);

And I get this for the first line above:Warning: array_rand() [function.array-rand]: Second argument has to be between 1 and the number of elements in the arraySo I decided to count the elements in the array, and it comes back to 0. Even though the path is correct, and the folder has about 50 images inside of it.

Link to comment
Share on other sites

You have to specify the complete path to the file, including the filename itself. That's why my example had the *.png mask. In your code, however, it would look for files of name $variablefoldername in the folder path/to/ (unless $variablefoldername had a forward slash in its value). If all the files in that folder are image files you can just write:

$rnd_image_names = array_rand(glob("path/to/$variablefoldername/*"), 4);$result = count($rnd_image_names);

Link to comment
Share on other sites

Oh whoops - those are the random indices selected by array_rand(), and need to be used to get the actual value from the glob() array.

$images = glob("path/to/$variablefoldername/*");$random = array_rand($images);foreach ($random as $n) {	do_something_with($images[$n]);}

Link to comment
Share on other sites

Ah ok.That Foreach statement produces an error:Warning: Invalid argument supplied for foreach()At first I thought it was what I put within the foreach, but commenting it all out, still made no difference and still the same error.When printing out the array of $random, there is just one number. How do I choose 4 (or whatever other number) random images? Could you also explain what each bit of the code does please?

Link to comment
Share on other sites

When printing out the array of $random, there is just one number. How do I choose 4 (or whatever other number) random images? Could you also explain what each bit of the code does please?
Same you did it before. Pass the number of elements you want as the second argument:$random = array_rand($images, 4);
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...