SitePoint Sponsor

User Tag List

Page 1 of 3 123 LastLast
Results 1 to 25 of 52

Thread: Open textfile

Hybrid View

  1. #1
    SitePoint Guru
    Join Date
    Sep 2007
    Posts
    971
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Open textfile

    Hello im after a code that will open all the .ini files inside a folder and output only the first line?

    cheers.
    Animated Chatrooms - www.121chatrooms.net

  2. #2
    SitePoint Evangelist hexburner's Avatar
    Join Date
    Jan 2007
    Location
    Belgium
    Posts
    591
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm sure you'll get it right if you were pointed in the right direction.
    Use opendir and readdir to read a directory and get a filelist.
    Then you can use pathinfo to see if the file has the .ini extension.
    Then use fopen and fgets to read the first line of the ini-file and do whatever you like with it.
    FOR SALE: 1 set of morals, never used, will sell cheap

  3. #3
    SitePoint Guru
    Join Date
    Sep 2007
    Posts
    971
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i can do all that its just im not sure with the get information from the firstline
    Animated Chatrooms - www.121chatrooms.net

  4. #4
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    To do this with a single file, the most beginner-friendly method is to use file() to load the file into an array and echo it's first element.

    PHP Code:
    <?php
    $lines 
    file('filename.ini');
    echo 
    $lines[0];
    ?>
    If the files are large, you would need to open a file resource and read and buffer char by char until you find a newline character.

    It's upto you to find out how to loop through the files and apply the above though.
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  5. #5
    SitePoint Guru
    Join Date
    Sep 2007
    Posts
    971
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hmmm thats a good question how would you loop it so it opens all .ini files
    Animated Chatrooms - www.121chatrooms.net

  6. #6
    SitePoint Guru
    Join Date
    Sep 2007
    Posts
    971
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i trying to loop with scandir but itsoutputting as > &#37;#HelpDesk.ini [10] => %#Chat-Lounge.txt [11] => %#Chat-Lounge.ini [12] => %#Adult-Spa.txt [13] => %#Adult-Spa.ini [14] => %#24.txt [15] => %#24.ini ) how can i make it output the names without the array:

    <?php
    $dir = '/tmp';
    $files1 = scandir($dir);
    $files2 = scandir($dir, 1);

    print_r($files1);
    print_r($files2);
    ?>
    Animated Chatrooms - www.121chatrooms.net

  7. #7
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    That's what print_r does.
    PHP Code:
    <?php
    $dir 
    '/tmp';
    $files scandir($dir1);
    echo 
    '<ul><li>' implode('</li><li>'$files) . '</li></ul>';
    ?>
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  8. #8
    SitePoint Guru
    Join Date
    Sep 2007
    Posts
    971
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hmmm im confused with that code i posted before is there a way to output just the filenames with no array stuff
    Animated Chatrooms - www.121chatrooms.net

  9. #9
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    What's wrong with the code I posted?
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  10. #10
    SitePoint Guru
    Join Date
    Sep 2007
    Posts
    971
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    just outputs 1 bullett
    Animated Chatrooms - www.121chatrooms.net

  11. #11
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    I updated it - what about now?
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  12. #12
    SitePoint Guru
    Join Date
    Sep 2007
    Posts
    971
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hmm cheers nearly got the hang of it now if i can combine the modified code with the first code so it opens every file in the folder and outputs only the firstline

    PHP Code:
     <?php $dir 't:/rooms'$files scandir($dir1); echo '' implode('<br>'$files) . ''?>
    and

    PHP Code:
    echo $lines[0]; 
    Animated Chatrooms - www.121chatrooms.net

  13. #13
    SitePoint Guru
    Join Date
    Sep 2007
    Posts
    971
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    anyone?
    Animated Chatrooms - www.121chatrooms.net

  14. #14
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    You haven't told us what your problem is.
    Last edited by Jake Arkinstall; Aug 30, 2008 at 06:22.
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  15. #15
    SitePoint Guru
    Join Date
    Sep 2007
    Posts
    971
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Im just trying to join those 2 codes together so its like a loop
    Animated Chatrooms - www.121chatrooms.net

  16. #16
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Can't you at least try to put it together?
    PHP Code:
    <?php
    $dir 
    '/tmp'
    $files scandir($dir1); 
    foreach(
    $files as $file){
        
    $lines file($file); 
        echo 
    $lines[0] . '<br />';
    }
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  17. #17
    SitePoint Guru
    Join Date
    Sep 2007
    Posts
    971
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    yer thats what i tried before well my code a little different but im just receiving blank page:

    PHP Code:
     <?php
     $dir 
    'T:/rooms';
     
    $files scandir($dir1);
     foreach(
    $files as $file){
     
    $lines file($file);
     echo 
    $lines[0] . '<br />';
     }
     
    ?>
    Animated Chatrooms - www.121chatrooms.net

  18. #18
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    That could mean there are no files in t:/rooms, or PHP doesn't have sufficient access.
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  19. #19
    SitePoint Guru
    Join Date
    Sep 2007
    Posts
    971
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    nah it wouldnt be that i even changed the address back to /tmp and didnt show anything it must be the code.
    Animated Chatrooms - www.121chatrooms.net

  20. #20
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Try this out.
    PHP Code:
    <?php
    $dir 
    '/tmp';
    if(!
    file_exists($dir) || !is_dir($dir)){
        die(
    'Folder "'.$dir.'" does not exist');
    }
    $files scandir($dir1); 
    if(
    $files === false){
        die(
    'Folder "'.$dir.'" cannot be opened');
    }
    echo 
    'Files found: in folder "' $dir '"<ul>';
    foreach(
    $files as $file){
        if(!
    is_dir($file)){
            echo 
    '<li>"'.$file.'" - First line: ';
            
    $lines file($dir '/' $file);
            if(
    $lines === false){
                echo 
    'File cannot be opened';
            }else{
                echo 
    htmlentities($lines[0]);
            }
        }
    }
    ?>
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  21. #21
    SitePoint Guru
    Join Date
    Sep 2007
    Posts
    971
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thank you heaps it works
    Animated Chatrooms - www.121chatrooms.net

  22. #22
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Updated it to be a bit more helpful in terms of erroring.
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  23. #23
    SitePoint Guru
    Join Date
    Sep 2007
    Posts
    971
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    cheers what php function would i use to output just the .ini files out of that folder
    Animated Chatrooms - www.121chatrooms.net

  24. #24
    SitePoint Guru
    Join Date
    Sep 2007
    Posts
    971
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    im also looking at the pathinfo function and it seems that filename dosnt work and all the rest do

    PHP Code:
    <?php
    $path_parts 
    pathinfo('/www/htdocs/index.html');

    echo 
    $path_parts['dirname'], "\n";
    echo 
    $path_parts['basename'], "\n";
    echo 
    $path_parts['extension'], "\n";
    echo 
    $path_parts['filename'], "\n"// since PHP 5.2.0
    ?>
    Animated Chatrooms - www.121chatrooms.net

  25. #25
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Just filter the array:
    PHP Code:
    <?php 
    function filter_ini($file){
        return (!
    is_dir($file) && preg_match('/(.*)\.ini/'$file));
    }
    $dir 'c:\\';
    if(!
    file_exists($dir) || !is_dir($dir)){ 
        die(
    'Folder "'.$dir.'" does not exist'); 

    if(
    substr($dir, -1) != '/'){
        
    $dir .= '/';
    }
    $files scandir($dir1); 
    if(
    $files === false){ 
        die(
    'Folder "'.$dir.'" cannot be opened'); 
    }
    $files array_filter($files'filter_ini');
    echo 
    'Files found in folder "' $dir '": '.count($files).'<ul>'
    foreach(
    $files as $file){ 
        echo 
    '<li>"'.$file.'" - First line: '
        
    $lines file($dir $file); 
        if(
    $lines === false){ 
            echo 
    'File cannot be opened'
        }else{ 
            echo 
    htmlentities($lines[0]); 
        } 
    }
    ?>
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona


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
  •