Laravel 4:追加数据到文件


Laravel 4: append data to file

我有"array()"文件,我需要从末尾附加特定字符的数据,例如:从end

附加到-3字符
<?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',")

,但如何在特定字符从结束追加?

谢谢,

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

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

我在Laravel外找到了答案:

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

PHP从指定位置追加到文件