i need to write a php variable at the top of php file after the php tah(
<?php开发者_运维问答
$test = "this is a string";
?>
After appending the variable, the above content shoud be like,
<?php
$subscriptionId = "123456789";
$test = "this is a string";
?>
Could not make it work using fwrite. Please suggest solution
I hope you're not describing an editor usage problem. Otherwise I'd recommend a regular expression to achieve the insertion:
$php = file_get_contents("example.php");
$php = preg_replace("/(<[?]php\s+)/", '$1 $subscriptionId = "123";\n', $php);
file_put_contents("example.php", $php);
There's no filesystem function for in-place insertion. You can only completely overwrite the php script.
Use str_replace() and don't omit to set the 4th parameter to 1, otherwise all occurrences of "<?php
" will be replaced:
$php = file_get_contents("example.php");
$php = str_replace("<?php", "<?php\n $subscriptionId = \"123456789\";\n", $php, 1);
file_put_contents("example.php", $php);
If you want to do it at the webserver level, there's the php.ini auto_prepend_file
parameter. With some judicious <File>/<Location> setup in a httpd.conf file, you can limit which .php files have this parameter applied.
精彩评论