Welcome Guest ( Log In | Register)



 
Reply to this topicStart new topic
> Display Random File In A Directory, how to display a random file from a set directory.
wappy
post Sep 10 2006, 09:34 AM
Post #1


Premium Member
********

Group: Members
Posts: 164
Joined: 2-July 06
From: England
Member No.: 25,974



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 unsure.gif
Go to the top of the page
 
+Quote Post
BuffaloHELP
post Sep 10 2006, 09:55 AM
Post #2


Desperately seeking "any key" to continue...
Group Icon

Group: Admin
Posts: 3,467
Joined: 23-April 05
From: Trap17 storage box
Member No.: 6,042



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

CODE

<?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 directory
closedir($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.

CODE
$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

CODE

<?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 directory
closedir($dir_handle);
?>
Go to the top of the page
 
+Quote Post
juice
post Sep 10 2006, 11:18 AM
Post #3


Premium Member
********

Group: Members
Posts: 183
Joined: 24-July 06
From: Cape Town
Member No.: 27,194



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.
QUOTE
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..
CODE

<?
//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....
CODE

::<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?
Go to the top of the page
 
+Quote Post
wappy
post Sep 10 2006, 11:42 AM
Post #4


Premium Member
********

Group: Members
Posts: 164
Joined: 2-July 06
From: England
Member No.: 25,974



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? :-)
Go to the top of the page
 
+Quote Post
juice
post Sep 10 2006, 11:49 AM
Post #5


Premium Member
********

Group: Members
Posts: 183
Joined: 24-July 06
From: Cape Town
Member No.: 27,194



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 smile.gif even thou I don't know a thing about PHP laugh.gif
Go to the top of the page
 
+Quote Post
beeseven
post Sep 10 2006, 04:06 PM
Post #6


Privileged Member
*********

Group: Members
Posts: 629
Joined: 26-February 05
Member No.: 3,995



QUOTE(BuffaloHELP @ Sep 10 2006, 05:55 AM) *
To show two random files

CODE
...
$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.
Go to the top of the page
 
+Quote Post
wappy
post Sep 10 2006, 05:23 PM
Post #7


Premium Member
********

Group: Members
Posts: 164
Joined: 2-July 06
From: England
Member No.: 25,974



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.
Go to the top of the page
 
+Quote Post
BuffaloHELP
post Sep 10 2006, 06:44 PM
Post #8


Desperately seeking "any key" to continue...
Group Icon

Group: Admin
Posts: 3,467
Joined: 23-April 05
From: Trap17 storage box
Member No.: 6,042



QUOTE(beeseven @ Sep 10 2006, 12:06 PM) *

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 smile.gif Thanks for the great modification. 6am reply posts aren't the greates quality tongue.gif
Go to the top of the page
 
+Quote Post
Saint_Michael
post Sep 10 2006, 10:20 PM
Post #9


$p4m 0n j00 $h4m3 m3 0nc3 $p4m 0n m3 $h4m3 m3 7\/\/1c3
*********************

Group: [HOSTED]
Posts: 6,448
Joined: 21-September 04
From: 9r33|\| 399$ 4|\|D 5P4/\/\
Member No.: 1,218
T17 GFX Crew



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://zulumonkey.org/?id=tutorials&pa...ment&oid=70