Jump to content

Recommended Posts

My code works but i need it to do something. the script add the data at the end of the document. what i would like is that, i want it to write at the top of the document without overwrite the actual document.

 

 


<?php




$text = $_GET['text'];
$act = $_GET['do'];
if ($act == "rst") {
$fp = fopen('log.txt', 'w+');
fwrite($fp, '');
fclose($fp);
}


else {


if ($act == "sub") {


$text = "<img src='images/say.png' height='20px'/><a href='#'>ZulfadlyAshBurn</a> " . $text . "<p style='float:right; color:#C0C0C0' align='right'><img ALIGN=ABSMIDDLE src='images/clock.png' height='20px'> " . date("d/m/y : H:i:s", time())  . "</img></p><hr color='#F0F0F0' width='100%' size='1'/>";


$fp = fopen('log.txt', 'a+');
fwrite($fp, $text);
fclose($fp);
readfile('log.txt');
}


else {


if ($act == "setp") {


$text = "<img src='images/set.png' height='20px'/><a href='#'>ZulfadlyAshBurn</a> " . $text. "<hr color='#F0F0F0' width='100%' size='1'/>";


$fp = fopen('log.txt', 'a+');
fwrite($fp, $text);
fclose($fp);
readfile('log.txt');
}
else {
echo "<title>Unauthorised</title>You are not authorised to view this page! Please leave.";
}
}
}


?>

Link to comment
https://forums.phpfreaks.com/topic/241587-help/
Share on other sites

Here's a small script to read and write to a text file.

a+ is append and w is write new

 

<?php
//read a file
$my_file = "filename.txt";
if (file_exists($my_file)) {
$data = file($my_file);
$total = count($data);
echo "<br />Total lines: $total<br />";
foreach ($data as $line) {
$line = trim($line);
echo "$line<br />";
}

//add a new line to end
$write = fopen($my_file, 'a+');
$message = "got a new line here\r\n";
fputs($write, $message);
fclose($write);

/*note the w, this will overwrite the entire contents
$write = fopen($my_file, 'w');
$message = "I just added this line and overwrote the file\r\n";
fputs($write, $message);
fclose($write);
*/

} else {
echo "No $my_file to display";
}
?>

Link to comment
https://forums.phpfreaks.com/topic/241587-help/#findComment-1240889
Share on other sites

Yeah sorry again, that appends but overwrites.

 

To really append at the beginning with no mess ups, you have to first get all the contents of the file (big files would consume lots of memory), save it something like temp.txt, clear the original file, put the new contents and append the saved contents at the end.

 

You can't just reverse the data when call upon the file?

Link to comment
https://forums.phpfreaks.com/topic/241587-help/#findComment-1240917
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.