real_illusions Posted January 18, 2011 Share Posted January 18, 2011 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 More sharing options...
justsomeguy Posted January 18, 2011 Share Posted January 18, 2011 What's the question? That code will print $folder if it's non-empty, or if it's empty it gets set to "./". Link to comment Share on other sites More sharing options...
real_illusions Posted January 18, 2011 Author Share Posted January 18, 2011 Well, it echos "random" and I dont see why, or how to change it to a folder of my choice (which is variable). Link to comment Share on other sites More sharing options...
justsomeguy Posted January 18, 2011 Share Posted January 18, 2011 According to that code, you're passing "random" as the name of the folder to that function. Link to comment Share on other sites More sharing options...
real_illusions Posted January 18, 2011 Author Share Posted January 18, 2011 But where does it define that? Theres nowhere in that code, nor elsewhere, where 'random' is defined as a folder. Link to comment Share on other sites More sharing options...
ShadowMage Posted January 18, 2011 Share Posted January 18, 2011 But where does it define that? Theres nowhere in that code, nor elsewhere, where 'random' is defined as a folder.Where is that function called? 'random' is somehow being passed into that function from someplace. Link to comment Share on other sites More sharing options...
real_illusions Posted January 18, 2011 Author Share Posted January 18, 2011 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 More sharing options...
Synook Posted January 18, 2011 Share Posted January 18, 2011 $rnd_image_names = array_rand(glob("folder/*.png"), 4)); Link to comment Share on other sites More sharing options...
real_illusions Posted January 19, 2011 Author Share Posted January 19, 2011 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 More sharing options...
Synook Posted January 19, 2011 Share Posted January 19, 2011 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 More sharing options...
real_illusions Posted January 19, 2011 Author Share Posted January 19, 2011 Ah...gotcha. Thanks! Link to comment Share on other sites More sharing options...
real_illusions Posted January 19, 2011 Author Share Posted January 19, 2011 Hmm...solve one problem to run into another.This produces numbers. How do I get the filenames from the array?For example, if the array is thisArray ( [0] => 2 [1] => 5 [2] => 18 [3] => 45 ) Link to comment Share on other sites More sharing options...
Synook Posted January 19, 2011 Share Posted January 19, 2011 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 More sharing options...
real_illusions Posted January 19, 2011 Author Share Posted January 19, 2011 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 More sharing options...
ShadowMage Posted January 19, 2011 Share Posted January 19, 2011 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 More sharing options...
real_illusions Posted January 19, 2011 Author Share Posted January 19, 2011 Ah, right. Finally getting there.Thanks for your help Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.