Results 1 to 15 of 15
  1. #1

    PHP : Reading text files ?

    Hi
    I need a help.

    This is the sample of Nginx web server configiraton.

    Code:
     location ~ \.php$ {
                root           html;
                fastcgi_pass   127.0.0.1:9000;
                fastcgi_index  index.php;
                fastcgi_param  SCRIPT_FILENAME  /usr/share/nginx/html$fastcgi_script_name;
                include        fastcgi_params;
            }

    I want to write a php script for getting the value of "root" , "fastcgi_pass" and "fastcgi_index" from the file.

    There may be hundreds of such blocks in the file and I want to collect all.

    Is that possible with php ? and how ?


    Harris

  2. #2
    Join Date
    Sep 2006
    Location
    Dallas, TX
    Posts
    333
    You can try opening the file with the fopen command:
    http://php.net/manual/en/function.fopen.php

    You will need to make sure the account running your PHP processor has access to the file.

    After that, you should be able to read and parse the file in your own code.

  3. #3
    Quote Originally Posted by NeilAgg View Post
    You can try opening the file with the fopen command:
    http://php.net/manual/en/function.fopen.php

    You will need to make sure the account running your PHP processor has access to the file.

    After that, you should be able to read and parse the file in your own code.
    Please read my question before making nonsense replies.
    I know how to open file, but I want to get certain values only.

  4. #4
    Join Date
    Sep 2006
    Location
    Dallas, TX
    Posts
    333
    In the original post, you did not state you were able to open the file so I assumed that is where you had the trouble.

    Regarding writing code to extract the contents, read the file line by line and look for "root" , "fastcgi_pass" and "fastcgi_index" on each line. If you see one of those, parse the value and store it in an array.

  5. #5
    For example,
    I want to get the value of "root" which is "html"

    Want to get the value of "fastcgi_pass" which is "127.0.0.1:9000"

    I also would like to know how to replace those values into the file for saving it with new values


    Something like THIS


    Harris

  6. #6
    try using google tizag.com/phpT/fileread.php be4 u start abusing members that r trying to help you

  7. #7
    Quote Originally Posted by loony View Post
    try using google tizag.com/phpT/fileread.php be4 u start abusing members that r trying to help you
    I am not trying to abuse any of the member. But that guys seemed like he is in rush and typed that reply without reading and understanding my question or may be my english is bad

    Anyways, I've explained more about it above, and you are also telling the same example. I know how to read the file, but I want to get values from the file, like php's ini parse



    Harris

  8. #8
    Join Date
    May 2009
    Posts
    766
    Quote Originally Posted by SunShellNET View Post
    Please read my question before making nonsense replies.
    I know how to open file, but I want to get certain values only.
    The title of your post is "PHP : Reading text files ?" It's not a stretch to assume you don't know how to read a text file from that. Don't be an ass to people who are trying to help you, because people like me who know the answer are just going to leave you hanging.

    *plonk*

  9. #9
    Quote Originally Posted by mattle View Post
    The title of your post is "PHP : Reading text files ?" It's not a stretch to assume you don't know how to read a text file from that. Don't be an ass to people who are trying to help you, because people like me who know the answer are just going to leave you hanging.

    *plonk*
    Alright. Never mind. It'll take some time, but I'll find it myself

    Harris

  10. #10

    what

    well if u went thru that page and read it properly u would see it shows u how to retrieve data using fget

  11. #11
    Quote Originally Posted by loony View Post
    well if u went thru that page and read it properly u would see it shows u how to retrieve data using fget
    Nop. That's not I wanted.

  12. #12
    $myFile = "testFile.txt";
    $fh = fopen($myFile, 'r');
    $root = fgets($root);
    $pass = fgets($fastcgi_pass );
    $index = fgets($fastcgi_index );
    fclose($fh);
    echo $root;
    echo $pass;
    echo $index;

    then change the txt file so that its a variable so $root = html;

  13. #13
    Quote Originally Posted by loony View Post
    then change the txt file so that its a variable so $root = html;
    What does that mean ?
    I can't change the file txt file content. Like it said, it is the configuration of Nginx Webserver. So if I change it then the web server may not work.

    Harris

  14. #14
    mattle , Just wanted to tell you that I got what I needed ( without your help :p )


    PHP Code:
    <pre>
    <?php
    $file 
    't.txt';
    $handle fopen($file"r");
    $string fread($handlefilesize($file));
    fclose($handle); 

    $pieces preg_split('/ /'$string, -1PREG_SPLIT_NO_EMPTY);
    print_r($pieces);
    ?>
       
    </pre>
    output

    Code:
    Array
    (
        [0] => location
        [1] => ~
        [2] => \.php$
        [3] => {
    
        [4] => root
        [5] => html;
    
        [6] => fastcgi_pass
        [7] => 127.0.0.1:9000;
    
        [8] => fastcgi_index
        [9] => index.php;
    
        [10] => fastcgi_param
        [11] => SCRIPT_FILENAME
        [12] => /usr/share/nginx/html$fastcgi_script_name;
    
        [13] => include
        [14] => fastcgi_params;
    
        [15] => }
    )

    And even better

    PHP Code:
    $pieces preg_split('/[ ;]/'$string, -1PREG_SPLIT_NO_EMPTY); 
    semi column removed

    Code:
    Array
    (
        [0] => location
        [1] => ~
        [2] => \.php$
        [3] => {
    
        [4] => root
        [5] => html
        [6] => 
    
        [7] => fastcgi_pass
        [8] => 127.0.0.1:9000
        [9] => 
    
        [10] => fastcgi_index
        [11] => index.php
        [12] => 
    
        [13] => fastcgi_param
        [14] => SCRIPT_FILENAME
        [15] => /usr/share/nginx/html$fastcgi_script_name
        [16] => 
    
        [17] => include
        [18] => fastcgi_params
        [19] => 
    
        [20] => }
    )
    Harris

  15. #15
    Join Date
    Mar 2009
    Posts
    2,222
    This may suffice, but it will also print out lines containing, say, "root2" and so on.

    Code:
    <?php
    
    $InputFile = $argv[1];
    $handle = @fopen($InputFile, "r");
    if ($handle) {
        while (!feof($handle)) {
            $InputLine = fgets($handle);
            if (substr_count($InputLine, 'root') > 0) print $InputLine;
            else if (substr_count($InputLine, 'fastcgi_pass') > 0) print $InputLine;
            else if (substr_count($InputLine, 'fastcgi_index') > 0) print $InputLine;
        }
        fclose($handle);
    }
    ?>

Similar Threads

  1. PHP cannot read Chinese Text Files
    By Gigaron in forum Programming Discussion
    Replies: 7
    Last Post: 11-19-2008, 03:14 AM
  2. Reading config files
    By BostonGuru in forum Programming Discussion
    Replies: 1
    Last Post: 11-20-2006, 01:25 AM
  3. php forms and arrays reading from text files
    By alpha883 in forum Programming Discussion
    Replies: 2
    Last Post: 06-06-2005, 05:02 AM
  4. PHP: Reading data from multiple text boxes?
    By frogb0x in forum Programming Discussion
    Replies: 2
    Last Post: 09-14-2004, 03:17 AM
  5. writing to and reading from files
    By billie493 in forum Programming Discussion
    Replies: 1
    Last Post: 03-20-2004, 01:46 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •