crashmaster Posted April 6, 2008 Share Posted April 6, 2008 Hi there, I need help. How can I write $string to file (.txt) to new line ?? I tried this, but it doesnt work: function ban_ip($ip) { $path = _path ('logs/bans.txt'); $str = '\n'.$ip.'#'.$_SERVER["SCRIPT_NAME"]; @$fp = fopen ($path, 'a+'); @fwrite($fp, $str); @fclose($fp); @@chmod ($path,0777); } Link to comment https://forums.phpfreaks.com/topic/99859-fopen-fwrite/ Share on other sites More sharing options...
GingerRobot Posted April 6, 2008 Share Posted April 6, 2008 You need to enclose special characters like new lines in double quotes. Link to comment https://forums.phpfreaks.com/topic/99859-fopen-fwrite/#findComment-510694 Share on other sites More sharing options...
crashmaster Posted April 6, 2008 Author Share Posted April 6, 2008 you mean all special chars line \t \n etc. should be in " " ?? like "\n" ?? Link to comment https://forums.phpfreaks.com/topic/99859-fopen-fwrite/#findComment-510697 Share on other sites More sharing options...
GingerRobot Posted April 6, 2008 Share Posted April 6, 2008 Yup. Otherwise php litterally writes \n to the screen. Link to comment https://forums.phpfreaks.com/topic/99859-fopen-fwrite/#findComment-510700 Share on other sites More sharing options...
crashmaster Posted April 6, 2008 Author Share Posted April 6, 2008 IT doesnt work... I will post you whole script here: function _path ($path) { $calls = 0; $calls ++; if($calls > 15) return FALSE; if (!file_exists($path)) { return _path('../'.$path); } else { return $path; } } function ban_ip($ip) { $lol = '/pages/add/message.php'; if ($_SERVER["SCRIPT_NAME"] != $lol) { $path = _path ('logs/bans.txt'); $str = "\n".$ip."#".$_SERVER["SCRIPT_NAME"]; @$fp = fopen ($path, 'a+'); @fwrite($fp, $str); @fclose($fp); @@chmod ($path,0777); } } bans.txt should be like this: 85.25.151.22#badscipt.php 85.25.151.22#badscipt.php 85.25.151.22#badscipt.php 85.25.151.22#badscipt.php 85.25.151.22#badscipt.php ... Link to comment https://forums.phpfreaks.com/topic/99859-fopen-fwrite/#findComment-510733 Share on other sites More sharing options...
hitman6003 Posted April 6, 2008 Share Posted April 6, 2008 try removing the "silencers" from the file write functions and see if they are giving any errors: $fp = fopen ($path, 'a+'); fwrite($fp, $str); fclose($fp); chmod ($path,0777); Also, are you viewing the contents of the file in your browser or in a text editor (vi, gedit, wordpad, etc)? Link to comment https://forums.phpfreaks.com/topic/99859-fopen-fwrite/#findComment-510736 Share on other sites More sharing options...
crashmaster Posted April 6, 2008 Author Share Posted April 6, 2008 Oh its ok.. F**ing notepad ) )If I open it in Rapid PHP its ok ) Thank you ) Link to comment https://forums.phpfreaks.com/topic/99859-fopen-fwrite/#findComment-510740 Share on other sites More sharing options...
hitman6003 Posted April 6, 2008 Share Posted April 6, 2008 yeah...notepad doesn't recognize "\n" as a new line...it must be "\r\n" (or maybe just "\r", but I don't think so) for notepad to recognize it. Link to comment https://forums.phpfreaks.com/topic/99859-fopen-fwrite/#findComment-510742 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.