Log in

View Full Version : php random text include from directories



Marquis
10-15-2011, 11:02 AM
Hello! For my Web site I have a number of directories that contain a number of text files with various bits of information that I wanted to display on my php pages. Ideally, I wanted to be able to include specifying the path to one or if possible to more of these folders; the function, then, would randomly choose one of the text files in the specified folder and return its contents.

keyboard
10-21-2011, 01:45 AM
http://www.dynamicdrive.com/forums/showthread.php?p=263347#post263347

This code should work fine


<?php
function RandomFile($folder='', $extensions=''){

// fix path:
$folder = trim($folder);
$folder = ($folder == '') ? './' : $folder;

// check folder:
if (!is_dir($folder)){ die('invalid folder given!'); }

// create files array
$files = array();

// open directory
if ($dir = @opendir($folder)){

// go trough all files:
while($file = readdir($dir)){

if (!preg_match('/^\.+$/', $file) and
preg_match('/\.('.$extensions.')$/', $file)){

// feed the array:
$files[] = $file;
}
}
// close directory
closedir($dir);
}
else {
die('Could not open the folder "'.$folder.'"');
}

if (count($files) == 0){
die('No files where found :-(');
}

// seed random function:
mt_srand((double)microtime()*1000000);

// get an random index:
$rand = mt_rand(0, count($files)-1);

// check again:
if (!isset($files[$rand])){
die('Array index was not found! very strange!');
}

// return the random file:
return $folder . $files[$rand];
}
include RandomFile('foldername/','txt');


You need to edit this bit


include RandomFile('foldername/','txt');[/

Change folder name to the name of the folder with your txt files in it.

djr33
10-21-2011, 03:03 AM
As discussed in your other thread, be very careful using include() like that. I think you want file_get_contents().

keyboard
10-21-2011, 04:32 AM
How would you change the include to use file_get_contents. I've never used it before.

djr33
10-21-2011, 04:36 AM
$stuff_from_file = file_get_contents($path);

You can also look on php.net for very detailed function descriptions.

include() grabs PHP code from a page and executes it. If that page happens to not include any PHP, then it will output it directly (into your html), but that's because it's really going through the PHP [but there isn't any]. This is a huge security risk though if you don't know what is in those files. (Especially if they are user generated.)

keyboard
10-21-2011, 06:52 AM
So instaed of this



include RandomFile('foldername/','txt');

You would have to put this



$stuff_from_file = file_get_contents(foldername/);


Is that right???

traq
10-21-2011, 02:17 PM
more like:
$stuff_from_file = file_get_contents('path/to/file.txt');
// when you're ready:
print $stuff_from_file;

keyboard
10-21-2011, 09:27 PM
But the script gets the address dynamically so you can't put in the location of the file.

djr33
10-21-2011, 09:44 PM
include RandomFile('foldername/','txt');



$stuff_from_file = file_get_contents(RandomFile('foldername/','txt'));
echo $stuff_from_file;

Marquis
12-04-2011, 11:41 PM
Thanks, it works fine.

is it also possible to scan subfolders and grab text files from ? How I Can Do that?