IPB

Welcome Guest ( Log In | Register )



Tags
This content has not been tagged yet
 
Reply to this topicStart new topic

Display Random File In A Directory

, how to display a random file from a set directory.


wappy
no avatar
Premium Member
********
Group: Members
Posts: 164
Joined: 2-July 06
From: England
Member No.: 25,974



Post #1 post Sep 10 2006, 09:34 AM
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
no avatar
More than meets the eye
******************
Group: Admin
Posts: 3,762
Joined: 23-April 05
From: Trap17 storage box
Member No.: 6,042
myCENT:49.00



Post #2 post Sep 10 2006, 09:55 AM
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
no avatar
Premium Member
********
Group: Members
Posts: 184
Joined: 24-July 06
From: Cape Town
Member No.: 27,194



Post #3 post Sep 10 2006, 11:18 AM
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
no avatar
Premium Member
********
Group: Members
Posts: 164
Joined: 2-July 06
From: England
Member No.: 25,974



Post #4 post Sep 10 2006, 11:42 AM
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
no avatar
Premium Member
********
Group: Members
Posts: 184
Joined: 24-July 06
From: Cape Town
Member No.: 27,194



Post #5 post Sep 10 2006, 11:49 AM
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
no avatar
Privileged Member
*********
Group: Members
Posts: 629
Joined: 26-February 05
Member No.: 3,995



Post #6 post Sep 10 2006, 04:06 PM
QUOTE(BuffaloHELP @ Sep 10 2006, 05:55 AM) [snapback]280599[/snapback]
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
no avatar
Premium Member
********
Group: Members
Posts: 164
Joined: 2-July 06
From: England
Member No.: 25,974



Post #7 post Sep 10 2006, 05:23 PM
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
no avatar
More than meets the eye
******************
Group: Admin
Posts: 3,762
Joined: 23-April 05
From: Trap17 storage box
Member No.: 6,042
myCENT:49.00



Post #8 post Sep 10 2006, 06:44 PM
QUOTE(beeseven @ Sep 10 2006, 12:06 PM) [snapback]280649[/snapback]

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
no avatar
$p4m 0n j00 $h4m3 m3 0nc3 $p4m 0n m3 $h4m3 m3 7\/\/1c3
*********************
Group: [MODERATOR]
Posts: 7,255
Joined: 21-September 04
From: 9r33|\| 399$ 4|\|D 5P4/\/\
Member No.: 1,218
myCENT:83.20



Post #9 post Sep 10 2006, 10:20 PM
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
Go to the top of the page
+Quote Post
wappy
no avatar
Premium Member
********
Group: Members
Posts: 164
Joined: 2-July 06
From: England
Member No.: 25,974



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

Reply to this topicStart new topic

Collapse

> Similar Topics

    Topic Title Replies Topic Starter Views Last Action
No New Posts   8 -Pandemonium- 9,300 25th August 2004 - 04:00 PM
Last post by: -Pandemonium-
No New Posts 5 BoSZ 10,306 8th January 2009 - 07:35 PM
Last post by: Arthur Dent
No New Posts 6 dozen 4,653 9th September 2004 - 11:58 PM
Last post by: Triple X
No New Posts   4 annylei 4,651 14th September 2004 - 09:38 PM
Last post by: Triple X
No New Posts 10 jailbox 4,907 19th August 2009 - 07:17 PM
Last post by: iworld200
No New Posts   6 XtremeGamer99 6,296 9th February 2005 - 10:48 AM
Last post by: alexwhin
No New Posts   3 X3r0X 4,652 1st October 2004 - 08:16 AM
Last post by: X3r0X
No New Posts   3 josh_sg1 4,240 12th October 2004 - 12:02 AM
Last post by: s2city
No New Posts   8 sohahm 5,139 21st October 2004 - 08:44 PM
Last post by: beg4mercy
No New Posts   0 sohahm 3,558 16th October 2004 - 01:46 AM
Last post by: sohahm
No New Posts   13 Lyon 13,656 9th February 2009 - 10:42 AM
Last post by: aloKNsh
No new   14 Lyon 7,551 1st November 2004 - 07:43 PM
Last post by: -3ad3oy-
No New Posts   1 liliano 3,349 29th October 2004 - 07:30 PM
Last post by: BoSZ
No New Posts   9 MuTeD 6,784 13th November 2004 - 12:22 PM
Last post by: no9t9
No New Posts   6 brainless 1,245 27th October 2006 - 04:49 AM
Last post by: quakesand


 



RSS Open Discussion Time is now: 8th November 2009 - 09:53 AM

Web Hosting Powered by ComputingHost.com.