Old 15-03-2004, 12:08   #1 (permalink)
Nasimov
Registered User
 
Join Date: Mar 2004
Location: EarthPlanet
Posts: 2
Writing to a file. (PHP)

It's possible to ammend data within a file vs. just adding data to the beginning or the end of the file? Mean to change something in the "middle" of the file.
Thanks.
  Reply With Quote
Old 15-03-2004, 12:46   #2 (permalink)
Brown
volkswagen yellow & gold
 
Brown's Avatar
 
Join Date: Apr 2003
Location: london, england.
Posts: 6,214
i don't understand what you're saying.
  Reply With Quote
Old 15-03-2004, 12:57   #3 (permalink)
Stickman
Dr. Lucien Sanchez
 
Stickman's Avatar
 
Join Date: Mar 2003
Location: UK
Posts: 5,632
The earthplanet man wants to edit (or post data to) the middle of a file. Not just tag stuff on the end.

I'm sure you can - but I don't know how.
  Reply With Quote
Old 15-03-2004, 13:00   #4 (permalink)
Brown
volkswagen yellow & gold
 
Brown's Avatar
 
Join Date: Apr 2003
Location: london, england.
Posts: 6,214
how about having three includes:

inc1.php
inc2.php
inc3.php

and just changing the middle one?
  Reply With Quote
Old 18-03-2004, 04:29   #5 (permalink)
And
Senior Member
 
And's Avatar
 
Join Date: Apr 2003
Posts: 103
Send a message via AIM to And Send a message via MSN to And
Take a look at PHP's string replacing functions - str_replace at php.net - you could also try using regular expressions to do the same thing depending on what you wanted to replace in your document.
  Reply With Quote
Old 18-03-2004, 07:19   #6 (permalink)
Brown
volkswagen yellow & gold
 
Brown's Avatar
 
Join Date: Apr 2003
Location: london, england.
Posts: 6,214
if you just want to replace part of an html page, then you could have a text file as an include which you then edit through php. that way, a section of text on a page can be edited and the html code is not touched.
  Reply With Quote
Old 19-03-2004, 09:42   #7 (permalink)
DaiWelsh
Registered User
 
Join Date: Mar 2004
Location: Derby, UK
Posts: 24
Quote:
Originally Posted by Nasimov
It's possible to ammend data within a file vs. just adding data to the beginning or the end of the file? Mean to change something in the "middle" of the file.
Thanks.

The standard approach is to read in the content of the file, alter it then write it back out. Generally you cannot edit the middle of the file without following this process unless the changes do not alter the number of bytes. To explain what I mean, imagine you have this in your file on disk

abcdefghijklmnop

and you want to change efg to ggffee. If you only read the middle part of the file (putting aside how you know where to read from) then tried to write the new version back at the same location in the file you would get

abcdggffeeklmnop

because what you wrote back was longer than the original and so would overwrite 'hij' as well, thus messing up the rest of the file.

If you happened to be writing back the same number of bytes e.g. 'gfe' then you could in theory write it directly over the original using fseek() to move to the correct part of the file before writing, but usually it is

read whole file
change it
write it back over the top of the original.

HTH,

Dai
  Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search


Contact Us - Web Design Forums - Archive - Top
Search Engine Optimization by vBSEO 3.0.0 RC8