You are here: Re: help please: insert string into file at specific location « PHP Programming Language « IT news, forums, messages
Re: help please: insert string into file at specific location

Posted by Erwin Moller on 11/01/05 12:29

help wrote:

> I want to open a file and write some string between the the 3rd line and
> the 4th line. my code (as follows) can insert after the 3rd line but
> somehow it owerwrites the 4th line. So i lost its original content. As my
> original file has
> more than 10000 lines of code, I'm looking for a handy way to do that.
> Does anyone know how I can fix it? Please help me. Thanks very much.
>
>
> $content = "new content here\n"
> $handle = fopen("test.wrl", "r+"); //open file
> $theData = "";
> $i = 1;
> while($i<4){
> $theData += fgets($handle);
> $i++;
> }
> fputs($handle, $content); //write
> fclose($handle); //close it

Hi,

AFAIK fopen doesn't have an 'insert'-mode.
Probably the easiest way to code this is to read the whole file in memory,
and write it back.
Have a look at the function file() at www.php.net which returns an array of
all lines.

Regards,
Erwin Moller

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация