Results 1 to 10 of 10

Thread: php random text include from directories

  1. #1
    Join Date
    Jul 2007
    Posts
    34
    Thanks
    6
    Thanked 1 Time in 1 Post

    Default php random text include from directories

    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.

  2. #2
    Join Date
    Mar 2011
    Posts
    2,144
    Thanks
    59
    Thanked 116 Times in 113 Posts
    Blog Entries
    4

    Default

    http://www.dynamicdrive.com/forums/s...347#post263347

    This code should work fine

    HTML Code:
    <?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

    HTML Code:
    include RandomFile('foldername/','txt');[/
    Change folder name to the name of the folder with your txt files in it.
    Last edited by keyboard; 10-21-2011 at 06:52 AM.

  3. #3
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    As discussed in your other thread, be very careful using include() like that. I think you want file_get_contents().
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  4. #4
    Join Date
    Mar 2011
    Posts
    2,144
    Thanks
    59
    Thanked 116 Times in 113 Posts
    Blog Entries
    4

    Default

    How would you change the include to use file_get_contents. I've never used it before.

  5. #5
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    $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.)
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  6. #6
    Join Date
    Mar 2011
    Posts
    2,144
    Thanks
    59
    Thanked 116 Times in 113 Posts
    Blog Entries
    4

    Default

    So instaed of this


    PHP Code:
    include RandomFile('foldername/','txt'); 
    You would have to put this

    PHP Code:
    $stuff_from_file file_get_contents(foldername/); 
    Is that right???

  7. #7
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    more like:
    PHP Code:
    $stuff_from_file file_get_contents('path/to/file.txt');
    // when you're ready:
    print $stuff_from_file

  8. #8
    Join Date
    Mar 2011
    Posts
    2,144
    Thanks
    59
    Thanked 116 Times in 113 Posts
    Blog Entries
    4

    Default

    But the script gets the address dynamically so you can't put in the location of the file.

  9. #9
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    PHP Code:
    include RandomFile('foldername/','txt'); 
    PHP Code:
    $stuff_from_file file_get_contents(RandomFile('foldername/','txt'));
    echo 
    $stuff_from_file
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  10. The Following User Says Thank You to djr33 For This Useful Post:

    Marquis (12-04-2011)

  11. #10
    Join Date
    Jul 2007
    Posts
    34
    Thanks
    6
    Thanked 1 Time in 1 Post

    Default

    Thanks, it works fine.

    is it also possible to scan subfolders and grab text files from ? How I Can Do that?
    Last edited by Marquis; 12-11-2011 at 02:03 PM.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •