Results 1 to 8 of 8

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

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

    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 keyboard; 10-21-2011 at 01:40 AM.

  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
    Posts
    2,144
    Thanks
    59
    Thanked 116 Times in 113 Posts
    Blog Entries
    4

    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');

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

    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?

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

    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

  6. #6
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 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>| 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

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

    Default

    ...especially when you're finding the file dynamically.

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

    Default

    Thanks for all your help guys!!!

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
  •