Advanced Search

Results 1 to 8 of 8

Thread: php random file include, can't find file

  1. #1
    Join Date
    Mar 2011
    Location
    N 11° 19' 0.0012 E 142° 15' 0
    Posts
    1,420
    Thanks
    37
    Thanked 84 Times in 83 Posts
    Blog Entries
    3

    Default php random file include, can't find file

    Hello everyone,
    I was browsing the internet and found this code

    PHP Code:
    <?php
    function RandomFile($folder='tmp'$extensions='txt'){
     
        
    // 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(0count($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('tmp/','txt');


    I tried it ( the folder that the files are in is called tmp and they are all text files) but it came up with this


    HTML Code:
    ( ! ) Warning: scandir(/tmp,/tmp) [function.scandir]: The system cannot find the file specified. (code: 2) in C:\Documents and Settings\Owner\Desktop\canberra amatuer productions\www\scandir\tmp\hi.txt on line 3 
    Call Stack 
    # Time Memory Function Location 
    1 0.0004 375896 {main}( ) ..\random.php:0 
    2 0.0007 379976 include( 'C:\Documents and Settings\Owner\Desktop\canberra amatuer productions\www\scandir\tmp\hi.txt' ) ..\random.php:52 
    3 0.0007 380104 scandir ( ) ..\hi.txt:3 
    
    ( ! ) Warning: scandir(/tmp) [function.scandir]: failed to open dir: No such file or directory in C:\Documents and Settings\Owner\Desktop\canberra amatuer productions\www\scandir\tmp\hi.txt on line 3 
    Call Stack 
    # Time Memory Function Location 
    1 0.0004 375896 {main}( ) ..\random.php:0 
    2 0.0007 379976 include( 'C:\Documents and Settings\Owner\Desktop\canberra amatuer productions\www\scandir\tmp\hi.txt' ) ..\random.php:52 
    3 0.0007 380104 scandir ( ) ..\hi.txt:3 
    
    ( ! ) Warning: scandir() [function.scandir]: (errno 2): No such file or directory in C:\Documents and Settings\Owner\Desktop\canberra amatuer productions\www\scandir\tmp\hi.txt on line 3 
    Call Stack 
    # Time Memory Function Location 
    1 0.0004 375896 {main}( ) ..\random.php:0 
    2 0.0007 379976 include( 'C:\Documents and Settings\Owner\Desktop\canberra amatuer productions\www\scandir\tmp\hi.txt' ) ..\random.php:52 
    3 0.0007 380104 scandir ( ) ..\hi.txt:3 
    
    ( ! ) Warning: scandir(/tmp,/tmp) [function.scandir]: The system cannot find the file specified. (code: 2) in C:\Documents and Settings\Owner\Desktop\canberra amatuer productions\www\scandir\tmp\hi.txt on line 4 
    Call Stack 
    # Time Memory Function Location 
    1 0.0004 375896 {main}( ) ..\random.php:0 
    2 0.0007 379976 include( 'C:\Documents and Settings\Owner\Desktop\canberra amatuer productions\www\scandir\tmp\hi.txt' ) ..\random.php:52 
    3 0.0012 380424 scandir ( ) ..\hi.txt:4 
    
    ( ! ) Warning: scandir(/tmp) [function.scandir]: failed to open dir: No such file or directory in C:\Documents and Settings\Owner\Desktop\canberra amatuer productions\www\scandir\tmp\hi.txt on line 4 
    Call Stack 
    # Time Memory Function Location 
    1 0.0004 375896 {main}( ) ..\random.php:0 
    2 0.0007 379976 include( 'C:\Documents and Settings\Owner\Desktop\canberra amatuer productions\www\scandir\tmp\hi.txt' ) ..\random.php:52 
    3 0.0012 380424 scandir ( ) ..\hi.txt:4 
    
    ( ! ) Warning: scandir() [function.scandir]: (errno 2): No such file or directory in C:\Documents and Settings\Owner\Desktop\canberra amatuer productions\www\scandir\tmp\hi.txt on line 4 
    Call Stack 
    # Time Memory Function Location 
    1 0.0004 375896 {main}( ) ..\random.php:0 
    2 0.0007 379976 include( 'C:\Documents and Settings\Owner\Desktop\canberra amatuer productions\www\scandir\tmp\hi.txt' ) ..\random.php:52 
    3 0.0012 380424 scandir ( ) ..\hi.txt:4 

    Scandir is the folder that the file is in (random.php).

    Any help?
    Last edited by keyboard1333; 10-21-2011 at 01:40 AM.
    keebs - keyboard1333 [at] gmail [dot] com
    Anime Views Forums

  2. #2
    Join Date
    Oct 2011
    Location
    In a server burrow!
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Get rid of =='tmp/' and =='txt'

    Should work

  3. #3
    Join Date
    Mar 2011
    Location
    N 11° 19' 0.0012 E 142° 15' 0
    Posts
    1,420
    Thanks
    37
    Thanked 84 Times in 83 Posts
    Blog Entries
    3

    Default

    Do you mean it should look like this

    PHP Code:
    <?php
    function RandomFile(){
     
        
    // 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(0count($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('tmp/','txt');
    keebs - keyboard1333 [at] gmail [dot] com
    Anime Views Forums

  4. #4
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,138
    Thanks
    61
    Thanked 438 Times in 427 Posts
    Blog Entries
    7

    Default

    The code you posted does not use the scandir() function, but the error messages you provide are all related to scandir().

    Obviously, this is either not the output from the code you posted, or there is more to the script than you have posted here. Could you please double-check, and post your entire code?

    Quote Originally Posted by keyboard1333 View Post
    Hello everyone,
    I was browsing the internet and found this code

    PHP Code:
    <?php
    function RandomFile($folder='tmp'$extensions='txt'){
     
        
    // 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(0count($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('tmp/','txt');


    I tried it ( the folder that the files are in is called tmp and they are all text files) but it came up with this


    HTML Code:
    ( ! ) Warning: scandir(/tmp,/tmp) [function.scandir]: The system cannot find the file specified. (code: 2) in C:\Documents and Settings\Owner\Desktop\canberra amatuer productions\www\scandir\tmp\hi.txt on line 3 
    Call Stack 
    # Time Memory Function Location 
    1 0.0004 375896 {main}( ) ..\random.php:0 
    2 0.0007 379976 include( 'C:\Documents and Settings\Owner\Desktop\canberra amatuer productions\www\scandir\tmp\hi.txt' ) ..\random.php:52 
    3 0.0007 380104 scandir ( ) ..\hi.txt:3 
    
    ( ! ) Warning: scandir(/tmp) [function.scandir]: failed to open dir: No such file or directory in C:\Documents and Settings\Owner\Desktop\canberra amatuer productions\www\scandir\tmp\hi.txt on line 3 
    Call Stack 
    # Time Memory Function Location 
    1 0.0004 375896 {main}( ) ..\random.php:0 
    2 0.0007 379976 include( 'C:\Documents and Settings\Owner\Desktop\canberra amatuer productions\www\scandir\tmp\hi.txt' ) ..\random.php:52 
    3 0.0007 380104 scandir ( ) ..\hi.txt:3 
    
    ( ! ) Warning: scandir() [function.scandir]: (errno 2): No such file or directory in C:\Documents and Settings\Owner\Desktop\canberra amatuer productions\www\scandir\tmp\hi.txt on line 3 
    Call Stack 
    # Time Memory Function Location 
    1 0.0004 375896 {main}( ) ..\random.php:0 
    2 0.0007 379976 include( 'C:\Documents and Settings\Owner\Desktop\canberra amatuer productions\www\scandir\tmp\hi.txt' ) ..\random.php:52 
    3 0.0007 380104 scandir ( ) ..\hi.txt:3 
    
    ( ! ) Warning: scandir(/tmp,/tmp) [function.scandir]: The system cannot find the file specified. (code: 2) in C:\Documents and Settings\Owner\Desktop\canberra amatuer productions\www\scandir\tmp\hi.txt on line 4 
    Call Stack 
    # Time Memory Function Location 
    1 0.0004 375896 {main}( ) ..\random.php:0 
    2 0.0007 379976 include( 'C:\Documents and Settings\Owner\Desktop\canberra amatuer productions\www\scandir\tmp\hi.txt' ) ..\random.php:52 
    3 0.0012 380424 scandir ( ) ..\hi.txt:4 
    
    ( ! ) Warning: scandir(/tmp) [function.scandir]: failed to open dir: No such file or directory in C:\Documents and Settings\Owner\Desktop\canberra amatuer productions\www\scandir\tmp\hi.txt on line 4 
    Call Stack 
    # Time Memory Function Location 
    1 0.0004 375896 {main}( ) ..\random.php:0 
    2 0.0007 379976 include( 'C:\Documents and Settings\Owner\Desktop\canberra amatuer productions\www\scandir\tmp\hi.txt' ) ..\random.php:52 
    3 0.0012 380424 scandir ( ) ..\hi.txt:4 
    
    ( ! ) Warning: scandir() [function.scandir]: (errno 2): No such file or directory in C:\Documents and Settings\Owner\Desktop\canberra amatuer productions\www\scandir\tmp\hi.txt on line 4 
    Call Stack 
    # Time Memory Function Location 
    1 0.0004 375896 {main}( ) ..\random.php:0 
    2 0.0007 379976 include( 'C:\Documents and Settings\Owner\Desktop\canberra amatuer productions\www\scandir\tmp\hi.txt' ) ..\random.php:52 
    3 0.0012 380424 scandir ( ) ..\hi.txt:4 

    Scandir is the folder that the file is in (random.php).

    Any help?
    Adrian ~ facebook | gist/github

    ['66.215.156.37','208.75.149.97'] // ip,ip array!
    "Take that sticker *off* your hat; you look stupid" --Wil Wheaton

  5. #5
    Join Date
    Mar 2011
    Location
    N 11° 19' 0.0012 E 142° 15' 0
    Posts
    1,420
    Thanks
    37
    Thanked 84 Times in 83 Posts
    Blog Entries
    3

    Default

    I worked out what was wrong. In the file hi.txt I had accidently put php code instead of normal text. because it was including it into the page it was executing the php code and stuffing the whole thing up. Thanks Traq, you were really helpfull
    keebs - keyboard1333 [at] gmail [dot] com
    Anime Views Forums

  6. #6
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    11,797
    Thanks
    227
    Thanked 657 Times in 645 Posts

    Default

    Don't include text like that. Use file_get_contents() to save the values to a string. As you've now learned, using include() can be dangerous.
    Daniel - Freelance Web Design | <?php?> | <html>| Deutsch | italiano | español | português | català | un peu de français | Ninasoma Kiswahili | 日本語の学生でした。| درست العربية

  7. #7
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,138
    Thanks
    61
    Thanked 438 Times in 427 Posts
    Blog Entries
    7

    Default

    ...especially when you're finding the file dynamically.
    Adrian ~ facebook | gist/github

    ['66.215.156.37','208.75.149.97'] // ip,ip array!
    "Take that sticker *off* your hat; you look stupid" --Wil Wheaton

  8. #8
    Join Date
    Mar 2011
    Location
    N 11° 19' 0.0012 E 142° 15' 0
    Posts
    1,420
    Thanks
    37
    Thanked 84 Times in 83 Posts
    Blog Entries
    3

    Default

    Thanks for all your help guys!!!
    keebs - keyboard1333 [at] gmail [dot] com
    Anime Views Forums

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
  •