Hi, im trying insert line to the end of file (data.php) and keep ?> in last place.
<?
user0:, 192.168.100.3, Prazdne, Novß ul.
user1:, 192.168.100.4, Jaro Vido,Novß ul.

?>

But i dont know how to insert line before ?>. Ex. of what i need: insert user2 following user1 and before ?>
im always getting

<?
user0:, 192.168.100.3, Prazdne, Novß ul.
user1:, 192.168.100.4, Jaro Vido,Novß ul.

?>
user2:, 192.168.100.5, PP A,Novß ul.

?>

Pls. help, peter

script writing writing:
<?php
$filename = 'users.php';
$somecontent = "user, IP, name, street". "?>";

// Let's make sure the file exists and is writable first.
if (is_writable($filename)) {
if (!$handle = fopen($filename, 'a+')) {
echo "Cannot open file ($filename)";
exit;
}

// Write $somecontent to our opened file.
if (fwrite($handle, $somecontent. "\n") === FALSE) {
echo "Cannot write to file ($filename)";
exit;
}

echo "Success, wrote ($somecontent) to file ($filename)";

fclose($handle);

} else {
echo "The file $filename is not writable";
}
?>

    i dont think thats possible.

    i suggest reading the whole of users.php file and store it in a variable, get rid of the php tags, <? and ?>, then add the new information on to the end, then re-add the tags.

    slightly long winded method, but in theory it works, and it's possible

      But since the data being added isn't PHP, there's no reason to have PHP tags at all (in fact, there's no reason why the file extension should be ".php"). Just having the lines of data would be sufficient, and then there'd be no ?> at the end to deal with.

        ...unless the file was inside the web root and the .php extension and tags were used to hide the contents of the file from a curious browser.

          kburger wrote:

          ...unless the file was inside the web root and the .php extension and tags were used to hide the contents of the file from a curious browser.

          In which case the file would immediately spit out errors likely containing some of the data you wanted to hide anyways because "user0" is not a valid php keyword or function...

          The best way to go about this is to place the file outside of the web accessible root and then just store it as a plain text file.

          Alternately, libmcrypt could be used to encrypt the data if it cannot be stored outside the webroot.

            rebelo wrote:

            In which case the file would immediately spit out errors likely containing some of the data you wanted to hide anyways because "user0" is not a valid php keyword or function...

            Yeah, you're right. But at least it wouldn't be viewable.

            rebelo wrote:

            The best way to go about this is to place the file outside of the web accessible root and then just store it as a plain text file.

            I totally agree.

              If it was 100% impossible to place it outside the root (i.e. on a shared server, you don't have that luxury due to host's restrictions, etc.), then you could place it in a secret directory that no one has access to (.htaccess file would have one line: Deny from all).

              That way, you can store it in a simple .txt file that is easy to read with PHP scripts, yet impossible to view (well, as impossible as the security vulnerabilities in your webserver, but you get what I mean).

                Write a Reply...