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 ?

Recommended Answers

All 2 Replies

Starting with the obvious, why are you using:

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

instead of:

$id = $lastid;

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.