SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Addict
    Join Date
    May 2004
    Location
    USA
    Posts
    342
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Including Random File.

    I'm pulling random files from a folder named 'video' and including them. I don't have the ability to hard code in the file names, so I'm using this:

    PHP Code:
    <?php $path11 'video/'
    $handle opendir ($path11);
    while ((
    $filename readdir ($handle)) !== false)
    {
    if (
    $filename != '.' && $filename != '..')
    {
    $file_array[] = $filename;
    }
    }

    $rand rand (0count ($file_array));
    include (
    $path11 $file_array[$rand]);  ?>
    I keep getting the following error (but it's not consistant - just sporadic)
    Code:
    
    Warning: main(video/) [function.main]: failed to open stream: No such file or directory in /home/xxxxxx/public_html/auto/xxxxx/randomvid.php on line 12
    
    Warning: main(video/) [function.main]: failed to open stream: No such file or directory in /home/xxxxx/public_html/auto/xxxxx/randomvid.php on line 12
    
    Warning: main() [function.include]: Failed opening 'video/' for inclusion (include_path='.:/usr/lib/php:/usr/local/lib/php') in /home/xxxxx/public_html/auto/xxxx/randomvid.php on line 12
    Line 12 is this line:
    include ($path11 . $file_array[$rand]);


    Right now there are only a handful of files in the video folder and they all have the same contents. Just can't figure out why I get the inconsistent failures.... any ideas? A way to fix it? Etc...

  2. #2
    SitePoint Member
    Join Date
    Jun 2007
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Have you tried echoing the include path to check that it's ok? Could show a really obvious error.

    Edit - Nvm, works fine on my dev server with PHP 5.2.2 and Apache 2.2.4

  3. #3
    SitePoint Addict
    Join Date
    May 2004
    Location
    USA
    Posts
    342
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Like I said it works fine 90 percent of the time or so. For example I just refreshed the page 10 times but got 3 errors out of those refreshes.

    Nothing I see "WRONG" with the code, so I figured it was my server acting up in some fashion. Tried another server and the same thing happens.

    I guess if no one see's anything wrong I'll build in an error message that just says "file not avail" at this time or something.

    Still be nice if someone could suggest something that works all the time.

  4. #4
    SitePoint Enthusiast
    Join Date
    May 2007
    Posts
    40
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Like I said it works fine 90 percent of the time or so. For example I just refreshed the page 10 times but got 3 errors out of those refreshes.
    Try echoing the path '$path11 . $file_array[$rand]' as Paulishness said and see what it says when you get errors.

  5. #5
    SitePoint Addict
    Join Date
    May 2004
    Location
    USA
    Posts
    342
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    When the error kicks up it isn't storing a file name.

    So for example when it works I get:

    video/podcast-video6.html

    When it doesn't work I just get:

    video/

    It's just returning a blank value occasionally for the $rand value.

  6. #6
    SitePoint Enthusiast
    Join Date
    May 2007
    Posts
    40
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    after while(), print_r($file_array) to confirm there is no empty value.

  7. #7
    SitePoint Addict
    Join Date
    May 2004
    Location
    USA
    Posts
    342
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I get the same value printing that out when I have a successful include and when I don't:

    Array ( [0] => podcast-video3.html [1] => podcast-video5.html [2] => podcast-video4.html [3] => podcast-video1.html [4] => podcast-video6.html [5] => podcast-video2.html )

    Through some additional error checking it's trying to include the array value of [6] on the errors which doesn't exist. So my guess is that this line:
    $rand = rand (0, count ($file_array)); is counting wrong in some fashion and adding an extra value. How would I get rid of that?

  8. #8
    SitePoint Enthusiast
    Join Date
    May 2007
    Posts
    40
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Use
    $rand = rand (0, count($file_array)-1);

  9. #9
    SitePoint Addict
    Join Date
    May 2004
    Location
    USA
    Posts
    342
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the help - guess that one was obvious once the error appeared


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
  •