Jump to content


Photo

fopen() help.


  • Please log in to reply
14 replies to this topic

#1 Zipp425

Zipp425
  • Members
  • PipPip
  • Member
  • 18 posts

Posted 14 August 2006 - 04:04 AM

I'm currently working on a javascript, xml, html, and php, based web application.
I was wondering if there was a way with php to open a file and delete a certain line and replace it. I am using PHP to create a xml file, and certain lines of that file will need to be deleted and replaced by changed data.

Any help or information on how to do this would be amazing. Let me know if you need more information.

Thanks!

zipp425@gmail.com

#2 simcoweb

simcoweb
  • Members
  • PipPipPip
  • Advanced Member
  • 1,102 posts
  • LocationCA

Posted 14 August 2006 - 06:02 AM

Below are the three basic ways to open a file and the corresponding character that PHP uses.

    * Read: 'r'

Open a file for read only use. The file pointer begins at the front of the file.

    * Write: 'w'

Open a file for write only use. In addition, the data in the file is erased and you will begin writing data at the beginning of the file. This is also called truncating a file, which we will talk about more in a later lesson. The file pointer begins at the start of the file.

    * Append: 'a'

There are additional ways to open a file. Above we stated the standard ways to open a file. However, you can open a file in such a way that reading and writing is allowable! This combination is done by placing a plus sign "+" after the file mode character.

    * Read/Write: 'r+'

Opens a file so that it can be read from and written to. The file pointer is at the beginning of the file.

    * Write/Read: 'w+'

This is exactly the same as r+, except that it deletes all information in the file when the file is opened.

    * Append: 'a+'

This is exactly the same as r+, except that the file pointer is at the end of the file.

Open a file for write only use. However, the data in the file is preserved and you begin will writing data at the end of the file. The file pointer begins at the end of the file.

#3 Zipp425

Zipp425
  • Members
  • PipPip
  • Member
  • 18 posts

Posted 14 August 2006 - 03:56 PM

Ok, I knew those things. Thank you.

Im wondering about deleting a specific line from a file though.

#4 Chetan

Chetan
  • Members
  • PipPipPip
  • Advanced Member
  • 162 posts
  • LocationIndia

Posted 14 August 2006 - 03:59 PM

You know the line number, then use file()
I am a PHP Guru, ask me questions if you want to

#5 Zipp425

Zipp425
  • Members
  • PipPip
  • Member
  • 18 posts

Posted 14 August 2006 - 04:05 PM

sorry if this is to much to ask, but can you show me a simple example?

#6 Chetan

Chetan
  • Members
  • PipPipPip
  • Advanced Member
  • 162 posts
  • LocationIndia

Posted 14 August 2006 - 04:25 PM

file.txt
Hello
Mello
Tello

file.php
<?php
$file=file('file.txt');
echo($file['1']); // Hello
echo($file['2']); // Mello
echo($file['3']); // Tello
?>

I am a PHP Guru, ask me questions if you want to

#7 Zipp425

Zipp425
  • Members
  • PipPip
  • Member
  • 18 posts

Posted 14 August 2006 - 04:29 PM

great. thank you!

#8 Chetan

Chetan
  • Members
  • PipPipPip
  • Advanced Member
  • 162 posts
  • LocationIndia

Posted 14 August 2006 - 04:30 PM

I think you know you cant append files with it, ull hav 2 code it so you get everything exept the line to append it
I am a PHP Guru, ask me questions if you want to

#9 simcoweb

simcoweb
  • Members
  • PipPipPip
  • Advanced Member
  • 1,102 posts
  • LocationCA

Posted 14 August 2006 - 04:34 PM

In order to delete OR append a specific line there needs to be a 'key' or an identifier like a line number.

$key = 'whateveryouwanttoreplace';


What file type is it?

#10 Chetan

Chetan
  • Members
  • PipPipPip
  • Advanced Member
  • 162 posts
  • LocationIndia

Posted 14 August 2006 - 04:35 PM

XML as he specifiedin his post >.>
I am a PHP Guru, ask me questions if you want to

#11 simcoweb

simcoweb
  • Members
  • PipPipPip
  • Advanced Member
  • 1,102 posts
  • LocationCA

Posted 14 August 2006 - 04:41 PM

Dohhhhhhhhhh!  That's one of those 'What color was George Washington's white horse?' kinda things

Ok, question. Will the data you want to change always be in the same location in the file or will the file constantly be changing in size, lines, etc.?

#12 Chetan

Chetan
  • Members
  • PipPipPip
  • Advanced Member
  • 162 posts
  • LocationIndia

Posted 14 August 2006 - 04:42 PM

No unless it is being changed somewhere else (locally or another script)
I am a PHP Guru, ask me questions if you want to

#13 simcoweb

simcoweb
  • Members
  • PipPipPip
  • Advanced Member
  • 1,102 posts
  • LocationCA

Posted 14 August 2006 - 04:53 PM

Then you couldn't use a line number because the line number could change if the file is modified and added to. So, you'd have to use a key which basically would be the string of text. Can you post an example of what would need to be replaced periodically? Please be as specific as you can with your example including any XML tags (if applicable).





#14 Zipp425

Zipp425
  • Members
  • PipPip
  • Member
  • 18 posts

Posted 14 August 2006 - 05:40 PM

Ive made it so the same data will always be on the same line.

I just need to figure out how to replace that specific line.

here is the xml file that is generated
[pre]
<?xml version='1.0' encoding='iso-8859-1'?>
<data>
<widgets>
<profile height='600px' width='800px' borderSize='2px' borderStyle='solid' borderColor='#000000' font='Arial, Helvetica, sans-serif' fontColor='#000000' fontSize='100%' backgroundColor='#ffffff' backgroundURL='' backgroundImgAlign='' backgroundImgRepeat='' misc=''/>
<textBox height='auto' width='300px' borderSize='1px' borderStyle='solid' borderColor='#000000' font='Arial, Helvetica, sans-serif' fontColor='#000000' fontSize='100%'  backgroundColor='#efefef' backgroundURL='' backgroundImgAlign='' backgroundImgRepeat='' misc='' />
<friendArea height='auto' width='400px' borderSize='1px' borderStyle='solid' borderColor='#000000' font='Arial, Helvetica, sans-serif' fontColor='#000000' fontSize='100%' backgroundColor='#efefef' backgroundURL='' backgroundImgAlign='' backgroundImgRepeat='' misc='' />
</widgets>
</data>
[/pre]

so basically what i need is to select the textBox line and replace it when a user changes the values.


#15 Zipp425

Zipp425
  • Members
  • PipPip
  • Member
  • 18 posts

Posted 14 August 2006 - 06:58 PM

Nevermind I got it all figured out. Thank you so much for your help.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users