chubbychubus Posted December 13, 2020 Share Posted December 13, 2020 (edited) i got the below code from google, need to adapt it to use, it works, but i just need it to over write on the file instead of appending to the file , please help, i am not a coder nor understand much of this, if someone could change it for me please: function logText($text) { $ts = date("D d-m-y h:i A",time()); $toLog = "[" . $ts . "] " . $text; $fh = fopen("logFile.txt", 'a'); fwrite($fh, $toLog."\n"); fclose($fh); } Edited December 13, 2020 by requinix Quote Link to comment https://forums.phpfreaks.com/topic/311856-chubbychub-no-this-thread-isnt-spam/ Share on other sites More sharing options...
requinix Posted December 13, 2020 Share Posted December 13, 2020 Look here. Read and try to understand. It's a very small change. But you really don't want to do that. It means only one message will ever be in the file at a time. The most recent message. Everything else got lost. So forget that code. Where did it come from? Whoever wrote it doesn't know about PHP, and getting PHP code some someone who doesn't know much about it is a pretty bad idea. What problem are you trying to solve? In unrelated news, I edited your thread title to put a little more effort into it than you did. Please try to write something useful in there next time. Quote Link to comment https://forums.phpfreaks.com/topic/311856-chubbychub-no-this-thread-isnt-spam/#findComment-1583023 Share on other sites More sharing options...
chubbychubus Posted December 13, 2020 Author Share Posted December 13, 2020 (edited) yeah, i am just to get my home computers post 1 line, so it can read 1 message. when an program starts, that way i know its running and what time it started. also, i read on that link prior to posting, but those modes are either prepend, or append to the file, not sure how to overwrite it though Edited December 13, 2020 by chubbychubus Quote Link to comment https://forums.phpfreaks.com/topic/311856-chubbychub-no-this-thread-isnt-spam/#findComment-1583024 Share on other sites More sharing options...
requinix Posted December 13, 2020 Share Posted December 13, 2020 Can you be a little more specific than that? What kind of program? What starts and stops it? Quote Link to comment https://forums.phpfreaks.com/topic/311856-chubbychub-no-this-thread-isnt-spam/#findComment-1583025 Share on other sites More sharing options...
chubbychubus Posted December 13, 2020 Author Share Posted December 13, 2020 its just a server script (in ahk) that starts a server and it will just post to that message, but i am not sure how its all going to work out, and just going step by step (so i dont have the full details of how i want the things to be yet) i could also login via temviewer and see if the program started, but this way i can just see it on the logs, instead of loginto my home computer( sometimes my wife uses it and dont need to interrupt her while in the middle of work) Quote Link to comment https://forums.phpfreaks.com/topic/311856-chubbychub-no-this-thread-isnt-spam/#findComment-1583026 Share on other sites More sharing options...
requinix Posted December 13, 2020 Share Posted December 13, 2020 So there's a small issue. If you just write when the server starts up, you can't actually know if it's still running. All the log really tells you is the last time it started - who knows if it shut down since then. But you say this is a server? Like an actual server running something you can connect to? Wouldn't the easiest way to know if it's running be to simply try to connect to it? Quote Link to comment https://forums.phpfreaks.com/topic/311856-chubbychub-no-this-thread-isnt-spam/#findComment-1583028 Share on other sites More sharing options...
chubbychubus Posted December 13, 2020 Author Share Posted December 13, 2020 no, its not an actual server, its just a server script, to receive a from a client script (via sockets)... but that is not relevant to this part of the script, i dont think. when the server script runs, it will post this message to let me know that its started the last time, (as you said).. i dont really need to see logs, just need to know the last time it started. so instead of appending or prepending to the file, i dont need to see all those logs, just the single one line. Quote Link to comment https://forums.phpfreaks.com/topic/311856-chubbychub-no-this-thread-isnt-spam/#findComment-1583031 Share on other sites More sharing options...
chubbychubus Posted December 13, 2020 Author Share Posted December 13, 2020 so is this possible? could someone just please modify that script to overwrite on the file? instead of appending or prepending? i tried "w" and "w+" ..and didnt seem to work. Quote Link to comment https://forums.phpfreaks.com/topic/311856-chubbychub-no-this-thread-isnt-spam/#findComment-1583033 Share on other sites More sharing options...
requinix Posted December 13, 2020 Share Posted December 13, 2020 I was on the phone, be patient. "w" should have done the job, but forget that code. The author doesn't know PHP. file_put_contents("logFile.txt", date("r")); That's all you need. If it's not creating the file then you're probably facing a file permissions issue; create the file yourself, change the permissions to 0666, and try running again. Quote Link to comment https://forums.phpfreaks.com/topic/311856-chubbychub-no-this-thread-isnt-spam/#findComment-1583034 Share on other sites More sharing options...
Solution chubbychubus Posted December 14, 2020 Author Solution Share Posted December 14, 2020 thank you requinix!, that helped, i ended up using the below code: function logText($text) { $ts = date("D d-m-y h:i A",time()); $toLog = "[" . $ts . "] " . $text; file_put_contents( "logFile.txt",$toLog); //fwrite($fh, $toLog."\n"); //fclose($fh); } this has time stamp in a formatt that i want to easily read. Quote Link to comment https://forums.phpfreaks.com/topic/311856-chubbychub-no-this-thread-isnt-spam/#findComment-1583040 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.