Andy-H Posted October 26, 2008 Share Posted October 26, 2008 Hello all, I am trying to teach myself the uses of fopen, fread and fwrite by writing a simple PHP/AJAX chat that writes and reads messages to and from a text file, I was just wondering what data needs to be escaped when using fwrite() and if there is a specific function to do this. Thanks for all/any replys. Link to comment https://forums.phpfreaks.com/topic/130195-solved-fwrite/ Share on other sites More sharing options...
trq Posted October 26, 2008 Share Posted October 26, 2008 No data needs to be esacped when writting to a file. Of course you may not want to allow html to be saved though. Link to comment https://forums.phpfreaks.com/topic/130195-solved-fwrite/#findComment-675191 Share on other sites More sharing options...
Andy-H Posted October 26, 2008 Author Share Posted October 26, 2008 OK thanks, I will be using htmlentities for storing and html_entity_decode for displaying, will this suffice to properly output the content? Link to comment https://forums.phpfreaks.com/topic/130195-solved-fwrite/#findComment-675195 Share on other sites More sharing options...
trq Posted October 26, 2008 Share Posted October 26, 2008 If you actually want to let html be stored there is no need to encode / decode it. Link to comment https://forums.phpfreaks.com/topic/130195-solved-fwrite/#findComment-675243 Share on other sites More sharing options...
Andy-H Posted October 26, 2008 Author Share Posted October 26, 2008 Warning: fopen(chat.txt) [function.fopen]: failed to open stream: Permission denied in /home/mafiaworld/domains/mafia-world.net/public_html/andy/chat.php on line 11 Does that mean fopen has been disabled? Link to comment https://forums.phpfreaks.com/topic/130195-solved-fwrite/#findComment-675244 Share on other sites More sharing options...
trq Posted October 26, 2008 Share Posted October 26, 2008 It likely means apache does not have write permissions to the directory you are trying to write to. Link to comment https://forums.phpfreaks.com/topic/130195-solved-fwrite/#findComment-675247 Share on other sites More sharing options...
Andy-H Posted October 26, 2008 Author Share Posted October 26, 2008 ok, thanks Link to comment https://forums.phpfreaks.com/topic/130195-solved-fwrite/#findComment-675248 Share on other sites More sharing options...
Andy-H Posted October 26, 2008 Author Share Posted October 26, 2008 changed permission to 777 and its working, thanks Link to comment https://forums.phpfreaks.com/topic/130195-solved-fwrite/#findComment-675252 Share on other sites More sharing options...
corbin Posted October 26, 2008 Share Posted October 26, 2008 OK thanks, I will be using htmlentities for storing and html_entity_decode for displaying, will this suffice to properly output the content? No that will not. First you're escaping it. Then you're undoing the escaping. Link to comment https://forums.phpfreaks.com/topic/130195-solved-fwrite/#findComment-675257 Share on other sites More sharing options...
Andy-H Posted October 26, 2008 Author Share Posted October 26, 2008 ok ill just encode it thanks Link to comment https://forums.phpfreaks.com/topic/130195-solved-fwrite/#findComment-675259 Share on other sites More sharing options...
Andy-H Posted October 26, 2008 Author Share Posted October 26, 2008 I have come across another problem, when I write the data to the file it overwrites the last part that was added, how can I make it add to the content of the file rather than overwrite it? My current code is: <?php header( 'Cache-Control: no-cache, must-revalidate' ); header( 'Expires: Mon, 26 Jul 1997 05:00:00 GMT' ); session_start(); $user = $_SESSION['user']; $msg = htmlentities($_GET['entry'], ENT_QUOTES); $file = 'chat.txt'; $f = fopen($file, 'w'); $str = '<tr><td width="75" align="left"> ' . $user . '</td><td width="125" align="left"> ' . date("d-m-y g:ia") . '</td><td width="650" align="left"> ' . $msg . '</td></tr>'; fwrite($f, $str); fclose($f); ?> Link to comment https://forums.phpfreaks.com/topic/130195-solved-fwrite/#findComment-675262 Share on other sites More sharing options...
trq Posted October 26, 2008 Share Posted October 26, 2008 $f = fopen($file, 'a'); Link to comment https://forums.phpfreaks.com/topic/130195-solved-fwrite/#findComment-675263 Share on other sites More sharing options...
Andy-H Posted October 26, 2008 Author Share Posted October 26, 2008 thanks worked perfectly Link to comment https://forums.phpfreaks.com/topic/130195-solved-fwrite/#findComment-675264 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.