获取.dat文件内容php

时间:2016-06-09 02:38:56

标签: php mysql database codeigniter

我有一个程序来上传包含codeigniter文本的.dat文件。

这里是上传表单的代码:

视图

<div class="panel panel-warning">
    <div class="panel-heading">
        <h3><i class="fa fa-list"></i> Upload</h3>
    </div>
    <div class="panel-body">    
        <?php echo form_open_multipart('report/coba_upload');?>
            <div class="form-group">
                <input type="hidden" class="form-control" name="id">
                <input type="file" name="file_1" size="20" class="form-control"/><br /><br />
                <button type="submit" name="btn_upload" class="btn btn-primary">UPLOAD</button>
            </div>
        </form>
    </div>
</div>

这里有一个控制器代码来处理上传:

function coba_upload(){
        $this->load->library('upload');
        $file = $_FILES['file_1']['name'];
        $file_loc = $_FILES['file_1']['tmp_name'];
        $folder = "./upload/honor/";
        $this->upload->do_upload("file_1");
        if (move_uploaded_file($file_loc, $folder . $file)) {
            $handle = fopen("./upload/honor/$file", "r");
            if ($handle) {
                while (($buffer = fgets($handle, 4096)) !== false) {
                    echo $buffer;
                }
                fclose($handle);
            }

        } else {
            echo 'upload failed';
        }
    }

这里是输入文件(.dat文件)的一个示例:

GLC002                                  LABA RUGI KONSOLIDASI                                                       PAGE:   1
    BANK XXX                             AS AT MAY 31, 2016                                            RUN DATE: 06/06/2016
                                                   0788 DIV. XXX

    --------------------------------------------------------------------------------------------------------------------------
    SANDI     NAMA REKENING                           SALDO VALAS DLM RUPIAH       SALDO DALAM RUPIAH         SALDO GABUNGAN
    --------------------------------------------------------------------------------------------------------------------------
              PENDAPATAN

    4.1       PENDAPATAN BUNGA                                                          28,012,902.00           28,012,902.00
    4.2       PENDAPATAN PROPISI & KOMISI
    4.3       KEUNTGN TRANSAKSI SURAT BERHARGA
    4.4       KEUNTGN/KRG TRANSAKSI VALAS & DERIVATIF
    4.8       PENDAPATAN OPERASIONAL LAINNYA
    4.9       PENDAPATAN NON OPERASIONAL                                               304,282,764.00          304,282,764.00
    --------------------------------------------------------------------------------------------------------------------------
              TOTAL PENDAPATAN                                                         332,295,666.00          332,295,666.00
    --------------------------------------------------------------------------------------------------------------------------

但输出变为一行

GLC002 LABA RUGI KONSOLIDASI PAGE: 1 BANK NEGARA INDONESIA AS AT MAY 31, 2016 RUN DATE: 06/06/2016 0788 DIV. ORGANIZATIONAL LEARNING -------------------------------------------------------------------------------------------------------------------------- SANDI NAMA REKENING SALDO VALAS DLM RUPIAH SALDO DALAM RUPIAH SALDO GABUNGAN -------------------------------------------------------------------------------------------------------------------------- PENDAPATAN 4.1 PENDAPATAN BUNGA 28,012,902.00 28,012,902.00 4.2 PENDAPATAN PROPISI & KOMISI 4.3 KEUNTGN TRANSAKSI SURAT BERHARGA 4.4 KEUNTGN/KRG TRANSAKSI VALAS & DERIVATIF 4.8 PENDAPATAN OPERASIONAL LAINNYA 4.9 PENDAPATAN NON OPERASIONAL 304,282,764.00 304,282,764.00 -------------------------------------------------------------------------------------------------------------------------- TOTAL PENDAPATAN 332,295,666.00 332,295,666.00 -------------------------------------------------------------------------------------------------------------------------- BEBAN 5.1 BEBAN BUNGA 5.2 BEBAN PROPISI & KOMISI 5.3 BEBAN PENYISIHAN KERUGIAN 6,333,036.01- 6,333,036.01- 5.5 BEBAN ADMINISTRASI DAN UMUM 62,029,442,518.19- 62,029,442,518.19- 5.6 KERUGIAN TRANSAKSI SURAT BERHARGA 5.7 KRG TRX VALAS DAN DERIVATIF 5.8 BEBAN OPERASIONAL LAINNYA 500,000.00- 500,000.00- 5.9 BEBAN NON OPERASIONAL -------------------------------------------------------------------------------------------------------------------------- TOTAL BIAYA 62,036,275,554.20- 62,036,275,554.20- -------------------------------------------------------------------------------------------------------------------------- LABA/RUGI TAHUN BERJALAN 61,703,979,888.20- 61,703,979,888.20- -------------------------------------------------------------------------------------------------------------------------- PT BANK XXX

问题是:

  1. 获取与原始文件相同的输出功能(不仅仅是一个 线)?
  2. 如何根据其位置(列和行)获取特定文本并将其转换为数组并插入数据库sql?
  3. 抱歉,如果这个问题太基本但我被困住了,没有得到我需要的东西。请给我一个解决方案。

    感谢您的关注和善意

1 个答案:

答案 0 :(得分:0)

您可以使用&lt; pre&gt; 标记

<pre>
<?php echo form_open_multipart('report/coba_upload');?>
</pre>

另一种方法是使用 nl2br

<?php echo nl2br(form_open_multipart('report/coba_upload'));?>