Jump to content
xisto Community
wappy

Display Random File In A Directory how to display a random file from a set directory.

Recommended Posts

hi, could someone please help me with this? I have some files in a directory and i want to know how i can randomly display link/s to one or more of the files in my directory for download. But it must not at any time display index.php which is also in the directory with the downloads.Thanks in advance for any help given :)

Share this post


Link to post
Share on other sites

Try this for a size. It was something I was using (not random) for my game league schedule list.

<?php$path = "../directory name of this index.php will reside/";$narray=array();$dir_handle = @opendir($path) or die("Unable to open $path please notify the administrator. Thank you.");echo "You can place greeting or some message here.\n";$i=0;while($file = readdir($dir_handle)){	 if($file != '.' && $file != '..' && $file !='index.php')	{		$narray[$i]=$file;		$i++;	}}$j = rand(0, $i-1);echo "<a target='_blank' href='$path$narray[$j]'>$narray[$j]</a>";//closing the directoryclosedir($dir_handle);?>
If all your files are in same extension, i.e. htm, then you can insert this code before the last echo to hide the extension.

$filename = str_replace(".htm", "", $narray[$j]);echo "<a target='_blank' href='$path$narray[$j]'>$filename</a>";

I think the rand(0, $i-1) is correct since when existing the loop, the last value of $i is greater than $narray[$i] = $file

To show two random files

<?php$path = "../directory name of this index.php will reside/";$narray=array();$dir_handle = @opendir($path) or die("Unable to open $path please notify the administrator. Thank you.");echo "You can place greeting or some message here.\n";$i=0;while($file = readdir($dir_handle)){	 if($file != '.' && $file != '..' && $file !='index.php')	{		$narray[$i]=$file;		$i++;	}}$j = rand(0, $i-1);$k = rand(0, $i-1);if ($j == $k){	   $k = rand(0, $i-1);}	   echo "<a target='_blank' href='$path$narray[$j]'>$narray[$j]</a><br />";echo "<a target='_blank' href='$path$narray[$k]'>$narray[$k]</a>";//closing the directoryclosedir($dir_handle);?>

Share this post


Link to post
Share on other sites

Wappy you could have just used your random text/image snipet and inserted links. I've come across a few sites were they do that.

Ok here is some code you can use for displaying random text, images, or links....//put this code on the page you wish to display the random text/image/link on..

<?//random code$xfile = @file("random.txt");$random_num = rand (0,count($xfile)-1);$udata = explode("::",$xfile[$random_num]);echo "<small>$udata[1]</small><br/>";?>
//create a text file like random.txt and put in it something like this....
::<a href="http://mradar.com/?i=22720">mRADAR.com</a>::<a href="http://surfwap.com/?site=neocult">SurfWAP.com</a>?>
This is the snippet from your site, can't you use it?

Share this post


Link to post
Share on other sites

many thanks buffalo :-) / but juice that would mean i would have to site there and add hundreds of links to a text file.. Its to add to my download script/site which auto displays the files with icons corresponding to the extension and by adding the manual random code would defeat the purpose of the script and ruin it. Why do hard work when your computer can do it for you? :-)

Share this post


Link to post
Share on other sites

Ok wappy, I miss understood you. How bout trying to use the random image script? Like the one I'm using for my avatar and signature bars? I'm sure it can be modified :P even thou I don't know a thing about PHP :)

Share this post


Link to post
Share on other sites

To show two random files

...$j = rand(0, $i-1);$k = rand(0, $i-1);if ($j == $k){	   $k = rand(0, $i-1);}	   ...?>
That should be modified. There's a chance (albeit a small one) that the second time you choose $k that it will be the same as $j again. Just change "if" to "while" to remedy this.

Share this post


Link to post
Share on other sites

thanks for the info :-) no i think i will just use this code juice, which random image code did you mean? The one i use is the same as my random link code with just images instead of links and a text file. It must read the files not me or the script user having to type them is boring and time wasting to type all that and the script is designed to be very simple to use with little or no setup needed.

Share this post


Link to post
Share on other sites

That should be modified. There's a chance (albeit a small one) that the second time you choose $k that it will be the same as $j again. Just change "if" to "while" to remedy this.

beeseven,

Ah ha! I knew something didn't look right :) Thanks for the great modification. 6am reply posts aren't the greates quality :P

Share this post


Link to post
Share on other sites

in my search for good scripts and what not i have an alternative although I havn't tested it myself it could be use to help not display the index page.

http://forums.xisto.com/no_longer_exists/

Share this post


Link to post
Share on other sites

ok thanks i will take a look at that tutorial. I think i can work out the index.php thing though as my download script already does that and my search but i built the base code so long ago i really can't remember how i done it lol, i been more busy with updating it with proper extension icons, search and the like. When/if i release it, it will be the best wap download script out, well it is already but i never give it out. Would like to say a big thanks to the creator of the wapbuddy script also for giving me a good starting point and inspiration to write the new download script :-)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.