I have problems to add variable in php file with inside array using fwrite
.
My php file has an array and is like this:
<?php
$array48= array (
0 =>
array (
0 => '2019-04-24-1248326-1313',
1 => 1556110847000,
2 => 30.6647,
3 => 71.387200000000007,
4 => 3.999999999999999,
5 => 51,
6 => 'O',
7 => 13,
8 => 0,
)
)
I want to add variable at first of file:
$fp_due_min_fa = fopen('../file.php', 'r+');
fwrite($fp_due_min_fa, '<?php ' . "\n" . '$id = ' . $id_terr . ';' . "\n");
fclose($fp_due_min_fa);
but after this code the php file is like this:
<?php
$id = 123456;
556110847000,
2 => 30.6647,
3 => 71.387200000000007,
4 => 3.999999999999999,
5 => 51,
6 => 'O',
7 => 13,
8 => 0,
)
)
The array is break...
I would to have this output:
<?php
$id = 123456;
$array48= array (
0 =>
array (
0 => '2019-04-24-1248326-1313',
1 => 1556110847000,
2 => 30.6647,
3 => 71.387200000000007,
4 => 3.999999999999999,
5 => 51,
6 => 'O',
7 => 13,
8 => 0,
)
)
Why I have this mistake?
The overall design looks bad, but in general just read, concatenate and write to pre-pend to the beginning. So as not to have to strip out the opening PHP tag, just open and close the tags:
$output = "<?php \$id = $id_terr; ?>\n";
$file = file_get_contents('../file.php');
file_put_contents('../file.php', $output.$file);
Or to append (assuming there is no closing PHP tag as shown), simply:
file_put_contents('../file.php', "\n\$id = $id_terr;\n", FILE_APPEND);