ZulfadlyAshBurn Posted July 10, 2011 Share Posted July 10, 2011 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."; } } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/241587-help/ Share on other sites More sharing options...
QuickOldCar Posted July 10, 2011 Share Posted July 10, 2011 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"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/241587-help/#findComment-1240889 Share on other sites More sharing options...
ZulfadlyAshBurn Posted July 10, 2011 Author Share Posted July 10, 2011 how is that suppose to put the new data at the top of the file? Quote Link to comment https://forums.phpfreaks.com/topic/241587-help/#findComment-1240892 Share on other sites More sharing options...
QuickOldCar Posted July 10, 2011 Share Posted July 10, 2011 Sorry, missed that part. http://php.net/manual/en/function.rewind.php Quote Link to comment https://forums.phpfreaks.com/topic/241587-help/#findComment-1240904 Share on other sites More sharing options...
ZulfadlyAshBurn Posted July 10, 2011 Author Share Posted July 10, 2011 HAHA. No Prob. Thanks alot. It work great Quote Link to comment https://forums.phpfreaks.com/topic/241587-help/#findComment-1240913 Share on other sites More sharing options...
ZulfadlyAshBurn Posted July 10, 2011 Author Share Posted July 10, 2011 sorry, my bad. somehow, it doesnt work or it just add to the end of doc. $fp = fopen('log.txt', 'a+'); rewind($fp); fwrite($fp, $text); fclose($fp); readfile('log.txt'); Quote Link to comment https://forums.phpfreaks.com/topic/241587-help/#findComment-1240915 Share on other sites More sharing options...
QuickOldCar Posted July 10, 2011 Share Posted July 10, 2011 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? Quote Link to comment https://forums.phpfreaks.com/topic/241587-help/#findComment-1240917 Share on other sites More sharing options...
ZulfadlyAshBurn Posted July 10, 2011 Author Share Posted July 10, 2011 ok. thx Quote Link to comment https://forums.phpfreaks.com/topic/241587-help/#findComment-1240928 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.