【问题标题】:Laravel 4: append data to fileLaravel 4:将数据附加到文件
【发布时间】:2014-09-25 12:06:31
【问题描述】:

我有“array()”文件,我需要从末尾追加特定字符中的数据,例如:从末尾追加到 -3 char

<?php
//arr_data.php

return array(
    'key1'  =>  'value1',
    'key2'  =>  'value2',
    'key3'  =>  'value3',
    'key4'  =>  'value4',

    //insert new data here
);

我知道,可以使用 Filesystem 附加到文件末尾:

Filesystem::append( "/dir/path", "'key5'    =>  'value5',")

但是如何从末尾追加特定的字符?

谢谢,

【问题讨论】:

    标签: php file laravel laravel-4


    【解决方案1】:

    您可以使用File::getRequire(); 来获取文件中数组的值,然后将数据附加到数组中并将其写回到同一个文件中。

    $data = File::getRequire('arr_data.php');
    $data[] = array('key5'    =>  'value5');
    $string = "<?php return" . var_export($data) . "; ?>";
    File::put('arr_data.php',$string );
    

    【讨论】:

    • 但我希望“arr_data.php”是一个非常大的数组
    【解决方案2】:

    我在 Laravel 之外找到了答案:

    <?php
      $file = fopen($filename, "c");
      fseek($file, -3, SEEK_END);
      fwrite($file, "whatever you want to write");
      fclose($file);
    ?>
    

    PHP appending to file from specific position

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-07-27
      • 2022-08-14
      • 2021-07-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多