mrkfox Posted December 2, 2009 Share Posted December 2, 2009 Hi I have a bit of a problem. Is there any way i can write some text to a middle of a text file. For instance on line 20? I know how to write text to the bottom of the file but not how i can get it to write in the middle. Any ideas? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/183725-writing-to-the-middle-of-a-file/ Share on other sites More sharing options...
rlelek Posted December 2, 2009 Share Posted December 2, 2009 What is the file being used for? Is the file used for data-storage (read/write by a computer)? or is it like a document where it's a bunch of text intended to be read by a human (and not a computer). If it's for data-storage, use a Database or XML (I guess you could use INI - ewwwwww). A Database would probably be the easiest. Quote Link to comment https://forums.phpfreaks.com/topic/183725-writing-to-the-middle-of-a-file/#findComment-969719 Share on other sites More sharing options...
mrkfox Posted December 2, 2009 Author Share Posted December 2, 2009 It is a system config file for my system to use. Once a user signs up i want them to be given a unique url by which they can access their user area. Basically a hosting service. I have to edit the hosts file in /etc/. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/183725-writing-to-the-middle-of-a-file/#findComment-969730 Share on other sites More sharing options...
Zane Posted December 2, 2009 Share Posted December 2, 2009 You should be able to do this with fseek. Fseek moves the file pointer to a certain location. You can get the middle of the file's location using filesize along with the above function... and divide it by 2. Use fwrite for writing to the file. And use fopen to open the file to begin with. example $theFile = fopen("myFile.txt", 'w'); fseek($theFile, (filesize($theFile)/2)); fwrite($theFile, "The middle of the file"); Quote Link to comment https://forums.phpfreaks.com/topic/183725-writing-to-the-middle-of-a-file/#findComment-969747 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.