jacko310592 Posted January 17, 2010 Share Posted January 17, 2010 hey guys, i made a code which detects whether its a new month since the last time my website logFile was modified, then if it is it will rename the file and create a new blank logFile, the only this is though is that its quite slow, it can somtimes take upto 2-3seconds to load on my home PC to load this one bit of code. anyway, here is the code, can anyone think of a way in which this could be made faster? $logfile = "{$ROOTdir}logs/logFile.txt"; $dateLastEdited = filemtime($logfile); if (date("F", $dateLastEdited) != date("F")){ $dateOfLastEdit = date("F y", $dateLastEdited); rename($logfile, "{$ROOTdir}logs/logFile ({$dateOfLastEdit}).txt"); $handle = fopen($logfile, 'a+'); fwrite($handle, $logline); fclose($handle); } else { $handle = fopen($logfile, 'a+'); fwrite($handle, $logline); fclose($handle); } Link to comment https://forums.phpfreaks.com/topic/188780-slow-logfile-date-check/ Share on other sites More sharing options...
Buddski Posted January 17, 2010 Share Posted January 17, 2010 Is it slow when creating a new log or appending to the old? Also file_put_contents()[/code] is probably a better method to writing to a file.(with the FILE_APPEND) flag. Link to comment https://forums.phpfreaks.com/topic/188780-slow-logfile-date-check/#findComment-996655 Share on other sites More sharing options...
jacko310592 Posted January 17, 2010 Author Share Posted January 17, 2010 it seem'd to happen whether it was creating the new file or appending the old, i changed the if (date("F", $dateLastEdited) != date("F")){ from a worded month to if (date("m", $dateLastEdited) != date("m")){ numbered moth, which weirdly speeded it up, now it only seems to go slighly slower (takes about 1sec to load) when creating the new file. but personally, i cannot see how the worded date can make it go any slower from the numbered one oh well, atleast it works well now. thanks for your post Buddski, ill have a look into 'file_put_contents' Link to comment https://forums.phpfreaks.com/topic/188780-slow-logfile-date-check/#findComment-996656 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.