HCProfessionals Posted October 4, 2011 Share Posted October 4, 2011 Ok, I know how to write to a file, but what I'm looking for is to check if a line of code exists, and if it is, don't recreate it. It would also be nice to not recreate the file either. $file = fopen("index.html", "w"); fwrite($file,"This is a line of text"); fclose($file); Quote Link to comment https://forums.phpfreaks.com/topic/248429-writing-to-a-file-help/ Share on other sites More sharing options...
HCProfessionals Posted October 4, 2011 Author Share Posted October 4, 2011 Now, how can I do checks to make sure the line of code doesn't already exist? $file = 'index.html'; $content = "This is a line of text"; file_put_contents($file, $content, FILE_APPEND | LOCK_EX); Quote Link to comment https://forums.phpfreaks.com/topic/248429-writing-to-a-file-help/#findComment-1275748 Share on other sites More sharing options...
requinix Posted October 4, 2011 Share Posted October 4, 2011 You can write to a file in-place but you'll only overwrite what is there (ie, there is no "insert" mode). If the size of the file could change then you have to recreate it. To check if the line exists you have to get all the lines and check if any of them are identical to your new line. Remember that file() keeps newlines on all the lines so you'll need to remove them before trying in_array(). Quote Link to comment https://forums.phpfreaks.com/topic/248429-writing-to-a-file-help/#findComment-1275783 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.