resnow Posted July 3, 2008 Share Posted July 3, 2008 I have a chunk of truly random data in a text file. I'd like my script to take the first two bytes of the data, store it, and then delete it from the file. This way the file would serve as a constant supply of fresh randomness. The reading part is easy, thanks to "fread", but I couldn't figure out how to delete just the first two bytes from the file. Help would be very much appreciated! Link to comment https://forums.phpfreaks.com/topic/113147-solved-how-to-eat-away-a-file/ Share on other sites More sharing options...
trq Posted July 3, 2008 Share Posted July 3, 2008 You will need to read the entire file into a string, take what you want using substr, then write the rest back using substr and fread. Link to comment https://forums.phpfreaks.com/topic/113147-solved-how-to-eat-away-a-file/#findComment-581289 Share on other sites More sharing options...
lemmin Posted July 3, 2008 Share Posted July 3, 2008 Just overwrite everything with the first two characters missing. I think it would be like this: $buffer = file_get_contents("file.txt"); file_put_contents("file.txt", substr_replace($buffer, substr($buffer, 2), 0); Link to comment https://forums.phpfreaks.com/topic/113147-solved-how-to-eat-away-a-file/#findComment-581291 Share on other sites More sharing options...
resnow Posted July 3, 2008 Author Share Posted July 3, 2008 Thanks guys! The script is now able to take what it needs from the file, and save the rest for later use. Problem solved. Link to comment https://forums.phpfreaks.com/topic/113147-solved-how-to-eat-away-a-file/#findComment-581343 Share on other sites More sharing options...
kenrbnsn Posted July 3, 2008 Share Posted July 3, 2008 If you're going to have many instances of the script running in parallel (i.e. it's web based), you may want to look at the flock() to ensure that only one instance of the script is reading/writing the file at a time. Ken Link to comment https://forums.phpfreaks.com/topic/113147-solved-how-to-eat-away-a-file/#findComment-581357 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.