通过PHP将文件内容读入表中

时间:2017-02-04 11:29:28

标签: php html file html-table

<?php
function lineNumber($file){
    $linecount = 0;
    $handle = fopen($file, "r");
    while(!feof($handle)){
        $line = fgets($handle);
        $linecount++;
    }
    fclose($handle);
    echo $linecount;
}

?>
<table class="table">
    <thead>
    <tr>
    <th>id</th>
    <th>subnet</th>
    <th>mask</th>
    <th>via</th>
    <th>option</th>

    </tr>
    </thead>
    <tbody>

    <?php
    $l1=lineNumber("other/network/route/via.txt");
for ($i=0; $i <$l1 ; $i++) { 
    $file="other/network/route/via.txt";

    $handle = fopen($file, "r");
    while(!feof($handle)){
        $line = fgets($handle);
        echo" 
            <tr>
                        <td>" '$line';"</td>
                        <td>John</td>
                        <td>Carter</td>
                        <td>johncarter@mail.com</td>
                        <td></td>
           </tr>
             ";
    }
}

fclose($handle);

?>  
</tbody>
</table>

我想读一些文本文件并将每行文件内容放到一行表中。每列都有一个文件。然后我有删除每一行的列,并删除文件中该行的内容。那我怎么能这样做呢?

1 个答案:

答案 0 :(得分:1)

$handle = fopen("other/network/route/via.txt", "r");
if ($handle) {
    while (($line = fgets($handle)) !== false) {
        // process the line read.
    }

    fclose($handle);
} else {
    // error opening the file.
}