0

have this script

$idhandle = fopen($filename, 'r+') or die("can't open file");
$id = "<?php \$lastid = \"$lastid\"; ?>";

fwrite($idhandle, $id);
fclose($idhandle);

trying to write "<?php \$lastid = \"$lastid\"; ?>"; in the first line of the file but what is happening is its deleting some of my code in the file. any way that you can skip a line or someting to preserve the coding that is already in the filename ?

3
Contributors
2
Replies
3
Views
8 Years
Discussion Span
Last Post by cwarn23
0

Starting with the obvious, why are you using:

$id = "<?php \$lastid = \"$lastid\"; ?>";

instead of:

$id = $lastid;
0

If you are trying to send php code to the file then perhaps it is better to use single quotes like the following so variables inside the string do not display their values:

$idhandle = fopen($filename, 'r+') or die("can't open file");
$id = '<?php $lastid = "$lastid"; ?>';

fwrite($idhandle, $id);
fclose($idhandle);

Or if you just want the value of the second variable substituted with its value while the first variable stays as it is:

$id = '<?php $lastid = "'.$lastid.'"; ?>';

Or if you want all of the variable names substituted with their values

$id = "<?php $lastid = \"$lastid\"; ?>";

It is unclear exactly what result you want in the text file but I hope the above examples help solve the question.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.