Reading files

Discussion in 'PHP' started by timelf123, Sep 8, 2007.

  1. #1
    I want to make a simple script that will write my new internet config for me.

    The syntax is like this:
    
    auto eth1:1
    iface eth1:1 inet static
    address XXX.XXX.XXX.XXX
    netmask 255.255.255.0
    
    Code (markup):
    So my code (never tried doing something like this, so I have no idea what to do lol) is this:

    
    <?php
    $ip = file_get_contents('ips.txt');
    $eth = file_get_contents('eth.txt');
    echo 'auto $eth \n';
    echo 'iface inet static \n';
    echo 'address $ip';
    echo 'netmask 255.255.255.0';
    ?>
    
    PHP:
    which obviously will not work. How can I have php make an array out of each line of those two files? The ips.txt contains the IPs I want to use, and the eth.txt contains the interface names, each line by line

    Thanks!
     
    timelf123, Sep 8, 2007 IP
  2. streety

    streety Peon

    Messages:
    321
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    0
    #2
    The file function will read a file and return the contents as an array where each element of the array corresponds to one line in the file.
     
    streety, Sep 8, 2007 IP
  3. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #3
    Plus, variables and new line characters won't be parsed between single quotes. Use double quotes instead.
     
    nico_swd, Sep 8, 2007 IP
  4. timelf123

    timelf123 Peon

    Messages:
    897
    Likes Received:
    24
    Best Answers:
    0
    Trophy Points:
    0
    #4
    So would I just make a foreach loop then? I don't know how to set up a loop that will loop all of my text, not just the contents of the file. I can get it so that it echoes the contents of the file, but I only want one line, then I need the whole thing to loop again


    eg:
     
    timelf123, Sep 8, 2007 IP