Results 1 to 6 of 6

Thread: need Help with random php script

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

    Default need Help with random php script

    Hello

    I use this script to random include file from a directory. I must declare all files that i want to choose from.
    Is it possible to change this that it includes files from a directory without that i must declare each single file?

    PHP Code:
    <?php
    srand
    ();
    $files = array("/1/file1.txt""/1/file2.txt""/1/file3.txt""/1/file4.txt","/1/file5.txt","/1/file6.txt");
    $rand array_rand($files);
    include (
    $files[$rand]);
    ?>
    Thank you, please excuse my bad english

  2. #2
    Join Date
    Jan 2015
    Posts
    78
    Thanks
    0
    Thanked 19 Times in 19 Posts

    Default

    See the php glob() function to get an array of the files within a folder.

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

    Default

    Thank you for your answer but i am not a coder... i don't have idea how to do what

  4. The Following User Says Thank You to Marquis For This Useful Post:

    jscheuer1 (05-03-2018)

  5. #4
    Join Date
    Jan 2015
    Posts
    78
    Thanks
    0
    Thanked 19 Times in 19 Posts

    Default

    Actually, you became a coder when you created/edited your file with the file names in it, regardless of how proficient you are.

    The php.net documentation for the glob() function defines what the input parameters are and what result the function returns, along with some examples.

    One of the fun points about programming is, you can try things and immediately see the result, thereby allowing you to learn if what you tried was correct or not.

    You need to make an attempt at solving this yourself and if you cannot solve it or get errors, post your code and the error or incorrect result, and someone will try to help you with the problem.

  6. #5
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,497
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    OK, the problem with glob for this is that it requires the relative path. What you have is perhaps an absolute network path:

    "/1/file2.txt"

    If that means (network), we go from the root of the domain, to a folder named 1, and look for .txt files there. If it's already relative, it means we go to a folder named 1 in the current directory and look for .txt files there.

    Glob can only do the latter. Do you know which you have?
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  7. #6
    Join Date
    Nov 2014
    Location
    On A Scottish Island
    Posts
    474
    Thanks
    0
    Thanked 61 Times in 57 Posts

    Default

    As long as the text files are all stored in the same directory and that directory doesn't contain any other files, then 'readdir()' would be the way to go.

Similar Threads

  1. Random Content Order script not so random?
    By robins in forum Dynamic Drive scripts help
    Replies: 3
    Last Post: 04-03-2009, 01:37 PM
  2. looking for php random image plus random alt from folder script
    By Marquis in forum Looking for such a script or service
    Replies: 0
    Last Post: 01-05-2009, 07:31 PM
  3. Random Content Order script - non-repeating random content
    By doomeyes in forum Dynamic Drive scripts help
    Replies: 1
    Last Post: 03-06-2008, 09:38 PM
  4. need help with script for random images
    By flyingfish in forum JavaScript
    Replies: 12
    Last Post: 03-14-2007, 06:36 AM
  5. random iframe script
    By HBUB69 in forum JavaScript
    Replies: 2
    Last Post: 12-24-2004, 03:41 AM

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
  •