Jump to content


Photo

File writing problems/optimization


  • Please log in to reply
4 replies to this topic

#1 royrules22

royrules22
  • Members
  • PipPip
  • Member
  • 11 posts

Posted 10 July 2006 - 11:14 PM

Hi,

This is my first time here, and I wish I found this place earlier. In any case I'm a begginner at PHP. I used to code almost daily last year, however due to school and other issues I'vent touched PHP in over 6 months. And now I need to make a script (in a few days no less), and I'm stumped on this issue.

This script will recieve data from a form and format it. Than it will open up an HTML form, go to a specified line and insert the formatted input there. (Basically a news script designed around the website, rather than otherway which I would've preffered).

My solution was to read the entire file into an array using file(), opening up the file in "w" mode (in effect erasing the entire thing), write array from begginning to line "x" onto file, write the formatted ouput, and than finish writing the rest of the array. Close file.

This works, however I dislike having to load a huge file onto an array (about 40Kb and will grow), and than writing back from the array line by line.

My question is if there a better way to solve the problem? As in is the a way to just navigate to line x and than write there instead of having to load the stuff into an array? I thought of opening in a+ mode, fseeking to the position and writing. However I seem to remember reading somewhere that it will ALWAYS write to the end of the file if opened in append mode.

Thanks for the help.

Oh and the server is an IIS not Apache which I am more comfortable with..

#2 royrules22

royrules22
  • Members
  • PipPip
  • Member
  • 11 posts

Posted 11 July 2006 - 06:58 PM

Anyone?

#3 ShogunWarrior

ShogunWarrior
  • Members
  • PipPipPip
  • Advanced Member
  • 528 posts
  • LocationIreland

Posted 11 July 2006 - 07:05 PM

You could think about doing this (PSEUDOCODE):
fopen file. //No data loaded.
loop{
//Read in one line and append it to a temporary one
//Change the line(s) you want and append
}
unlink() old file.
rename temp file to old file.

It seems roundabout but at least it doesn't have more than one line in memory at a time.
<a href="http://www.daviddora...nmedia.com/">My New Site/Blog</a> | <a href="http://www.daviddora...m/check/">Check your page for broken links/images/scripts</a>

Zend Certified Engineer
Follow me on Twitter: http://twitter.com/davidd

#4 royrules22

royrules22
  • Members
  • PipPip
  • Member
  • 11 posts

Posted 11 July 2006 - 07:35 PM

Hmm.. That would be a valid solution, but I still can't get over the fact that there's a lot of unnecessary extra resources being used (in this case the temp file). Isn't there a way to just seek to the link # and insert there?

#5 royrules22

royrules22
  • Members
  • PipPip
  • Member
  • 11 posts

Posted 11 July 2006 - 11:33 PM

Anyone else? Sorry for the second bump.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users