Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Nov 2006
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    random page generator - readdir()?

    I've been trying to tackle this problem on my own, but I dont have any PHP training, I've only survived in playing with code I've found opensource.

    I'm trying to find/write a script that, when clicked, will open a randomly selected HTML file in a directory (and within its subdirectories). It would not need to attempt to exclude other file types, as it is exclusively HTML.

    There are too many new pages for me to individually archive a set list, as some places have suggested. My research has told me that a readdir() method could solve this problem, but i dont know what to do with it

    Essentially, all of my files are in subdirectories as such:
    [Year Number]/[Month Number]/[unique file name].html

    Now, ideally, I could tell this script to search multiple folders, as pretty soon, 2007 is going to roll around and my files will be in two separate folders and their subdirectories. That is to say, I would tell it to search 2006 and 2007, and then within their subdirectories, and then randomly open an html file located in any of these folders. I'm afraid I know more about the generic implementation plan than actual coding. I could use all the help you can give.

  • #2
    ess
    ess is offline
    Regular Coder
    Join Date
    Oct 2006
    Location
    United Kingdom
    Posts
    866
    Thanks
    7
    Thanked 30 Times in 29 Posts
    Hello there.

    I have written a script to help you out. I have created a directory and called it pages. In the directory, I have created 6 html files. All files include one tag <h1>Page 1-6</h1>.

    Now the script. It automatically reads the directory and calculates the number of files in that directory. Please note that I have not specified that only HTML files should be included in the read operation. You should have ago at that yourself.

    here is the script

    PHP Code:
    <?php

    // specify which directory to read
    $dir "./pages";

    // open directory for read operation
    $handle opendir$dir );

    // obivously, you shouldn't include '.', and '..'
    // you can add more if you wish, for example
    // if you are on windows, you should exclude Thumb.db also
    $not_included = array( "."".." );

    // array to append files into
    $my_files = array();

    while( 
    $file readdir$handle ) )
    {
        
    // check that $file is not a directory
        // and that it has not been specified in $not_included array
        
    if( !is_dir$file ) && !in_array$file$not_included ) )
        {
            
    array_push$my_files$file );
        } 
    // ends if
    // ends while loop

    // close directory, we are done reading it
    closedir$handle );

    // calculate the number of files in the array
    $array_size count$my_files );

    // get a random number between 0 and the size of the array - 1
    $which_file rand0$array_size );

    // now include the file selected at random
    include $dir "/" $my_files$which_file ];

    ?>
    I hope that helps in getting your script going.

    All the best

  • #3
    New to the CF scene
    Join Date
    Nov 2006
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks a million for the script!

    I tested it, and it works for when I have all of my files in the directory, but its unable to successfully open files in the subdirectory.

    essentially, my folder layout is as follows:
    2006/09, 2006/10, 2006/11
    within each of those, there is only html files.

    so, if i set the folder as 2006/09, it would successfully pick a random file, but only from that folder. setting it to 2006, and trying to get it to search the subdirectories didnt work. so, i figured, ok, from the little programming i do know, maybe i can create a loop within the loop to open the subdirectory, add its contents to the array, and then close that subdirectory, then go back to iterating through the folder, repeating that when necessary. the added code looks as follows:

    PHP Code:


    while( $file readdir$handle ) )
    {
        
    // check that $file is not a directory
        // and that it has not been specified in $not_included array
        
    if( !is_dir$file ) && !in_array$file$not_included ) )
        {
            
    array_push$my_files$file );
        } 
    // ends if
        //if it is a directory, open it and add all of its subfiles into the array.
        
    if( is_dir($file) && !in_array$file$not_included )  ) 
        {
            
    $subhandleopendir($file);
            while( 
    $file readdir($subhandle)  )
             {
                 
    array_push($my_files$file);
              } 
    //ends while
            
    closedir($subhandle);
        } 
    //ends if
    // ends while loop


    // close directory, we are done reading it
    closedir$handle ); 
    but i'm still getting errors. i cant quite figure it out. the script is up at www.overheardatmcgill.com/random.php. please let me know what i'm doing wrong.

  • #4
    New Coder
    Join Date
    Feb 2010
    Posts
    38
    Thanks
    14
    Thanked 0 Times in 0 Posts
    How can I create a variable from this to use in same php file?

    include $dir . "/" . $my_files[ $which_file ];

    I tried something like this, but it doesn't work

    $newrandomtext = file_get_contents($dir . "/" . $my_files[ $which_file ]);
    echo $newrandomtext;

    It shows blanko page with PHP code in Source Code.. I need random text which is generated in those script pages...
    Thx in advance.
    Johny
    Last edited by young_coder; 02-12-2010 at 09:22 PM.


  •  

    Posting Permissions

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