473,775 Members | 2,365 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Read and Write file contents

pradeepjain
563 Contributor
Hii this is pradeep here . i am a newbie to PHP .can u help me out.
i hve a file called con.conf(config uration file) tht contains
name:pradeep
place:bangalore

i need to read this file and display the contents in form type in browser . and then gve a submit button so as to edit and update the file .
can u gve me a code for this process.
email:[ REMOVED ]
Jul 29 '07
59 4175
nathj
938 Recognized Expert Contributor
after reading from the form say $_post['name']
how to write back to file in same format as
name:pradeep
You need to try this for yourself, using the file functions referenced earlier. Have a go and post some code and the results if you run into trouble.

We are all prepared to help but we won't write the code for you.

Cheers
nathj
Jul 30 '07 #11
pradeepjain
563 Contributor
You need to try this for yourself, using the file functions referenced earlier. Have a go and post some code and the results if you run into trouble.

We are all prepared to help but we won't write the code for you.

Cheers
nathj

1st open the file in read mode
then take each line in a string using a for loop or so
then using explode searching for the : i wll split name:pradeep into 2 and then assign it some 2 variables;
and then print them in the form .
then use $_post method to read from the form and then write them to the file..

do u hve any suggstions on this
ok i am ready to try .but is the way i am going abt is it alright.
Jul 30 '07 #12
pradeepjain
563 Contributor
Srry If I Am Bothering U All
Jul 30 '07 #13
nathj
938 Recognized Expert Contributor
Srry If I Am Bothering U All
You are not bothering us all, this is what forums are for. It's just that the best way to learn is to have a go.

1st open the file in read mode
then take each line in a string using a for loop or so
then using explode searching for the : i wll split name:pradeep into 2 and then assign it some 2 variables;
and then print them in the form .
then use $_post method to read from the form and then write them to the file..

do u hve any suggstions on this
ok i am ready to try .but is the way i am going abt is it alright.
This process sounds okay to me, I think you are heading down the right road with this idea. Also it's good to plan the process out before you write so full marks there.

I look forward to seeing the result. I'm sure you'll get this working as you need it to and that you will understand what you have done and why it works.

Cheers
nathj
Jul 30 '07 #14
pradeepjain
563 Contributor
<?php
$file=fopen("/var/lib/penguin/Desktop/welcome.txt","r ")or exit("Unable to open file!");
$x=0;
for($i=0 ; $i != feof($file) ; $i++)
{
$data = fgets($file,$x) ;
$str = explode(':', $data);
$name[$i] = $str[0]; // piece1
$value[$i] = $str[1]; // piece2
echo $name[$i];
echo $value[$i];
$x=$x+1024;
$i=$i+1;
}
$lcount=$i;
?>
<html>
<head>
</head>
<body>
<?php
print $lcount;
?>
<FORM METHOD=post ACTION="test1.p hp">
for($i=0;$i <= $lcount ;$i++){
<input name= <?php $name[$i] ?> type=text > <?php $value[$i] ?>

}
<INPUT TYPE=SUBMIT>
</form>
</body>
</html>


i hve written this code...there is some error in the html combining wit the php..
can any one sujjest me something....
Jul 31 '07 #15
nathj
938 Recognized Expert Contributor
i hve written this code...there is some error in the html combining wit the php..
can any one sujjest me something....
Good to see you have written some code.

Try turning on the error hanlding and telling what you see on the page when you run it. This will shed some light on what is happening.

Also when yo say 'there is some error' what do you mean? What are you currently seeing, what do you think is happening.

My old university tutor would have sent me packing if I'd said 'there's some error please help.' You have to define the problem a bit more, especially as we cannot see your system/setup completely but just the code you post (thanks for posting it).

Let us know what comes back from your next set of tests.

Cheers
nathj
Jul 31 '07 #16
pradeepjain
563 Contributor
Good to see you have written some code.

Try turning on the error hanlding and telling what you see on the page when you run it. This will shed some light on what is happening.

Also when yo say 'there is some error' what do you mean? What are you currently seeing, what do you think is happening.

My old university tutor would have sent me packing if I'd said 'there's some error please help.' You have to define the problem a bit more, especially as we cannot see your system/setup completely but just the code you post (thanks for posting it).

Let us know what comes back from your next set of tests.

Cheers
nathj

oh ok....its not showning anything in the form jst form text box is appearing and then within it it appears
for($i=0;$i <= $lcount ;$i++){ type=text > } lke this in html page
Jul 31 '07 #17
gregerly
192 Recognized Expert New Member
First thing that jumps out at me is that you have PHP code not inside PHP tags:

[HTML]<FORM METHOD=post ACTION="test1.p hp">
for($i=0;$i <= $lcount ;$i++){
<input name= <?php $name[$i] ?> type=text > <?php $value[$i] ?>

}
<INPUT TYPE=SUBMIT>
</form>[/HTML]

which should be:

[HTML]<FORM METHOD=post ACTION="test1.p hp">[/HTML]
[PHP]<?
for($i=0;$i <= $lcount ;$i++){
<input name= <?php $name[$i] ?> type=text > <?php $value[$i] ?>

}
?>[/PHP]
[HTML]<INPUT TYPE=SUBMIT>
</form>[/HTML]

Greg
Jul 31 '07 #18
pradeepjain
563 Contributor
First thing that jumps out at me is that you have PHP code not inside PHP tags:

[HTML]<FORM METHOD=post ACTION="test1.p hp">
for($i=0;$i <= $lcount ;$i++){
<input name= <?php $name[$i] ?> type=text > <?php $value[$i] ?>

}
<INPUT TYPE=SUBMIT>
</form>[/HTML]

which should be:

[HTML]<FORM METHOD=post ACTION="test1.p hp">[/HTML]
[PHP]<?
for($i=0;$i <= $lcount ;$i++){
<input name= <?php $name[$i] ?> type=text > <?php $value[$i] ?>

}
?>[/PHP]
[HTML]<INPUT TYPE=SUBMIT>
</form>[/HTML]

Greg

i tried wht u told but not working
error:type=text > } ?>
Jul 31 '07 #19
nathj
938 Recognized Expert Contributor
Hi,

Try changing this section

[PHP]
<?
for($i=0;$i <= $lcount ;$i++){
<input name= <?php $name[$i] ?> type=text > <?php $value[$i] ?>

}
?>

[/PHP]

To something more like this:
[PHP]
<?php
for ($i=0; $i <= $lcount; $i++)
{
echo '<input type="text" name=' . $name[$i] . 'value=' . $value[$i] . ' />' ;
}

?>
[/PHP]
The first sample has nested php tags, I don't know if this is a problem or not but it sure looks weird. Also the raw HTML inside the php tags is a potential problem.
The second sample removes the need for this and should work just fine, assuming that the two arrays are correct.

So, your final page could look like this (all in php):
[PHP]
<?php
echo '<form name = "testform" method="post" action="test1.p hp">
';
for ($i=0; $i <= $lcount; $i++)
{
echo '<input type="text" name=' . $name[$i] . 'value=' . $value[$i] . ' />' ;
}
echo '<input type="submit" value="Complete "/>';
echo '</form>';
?>
[/PHP]


Have a play with this and let us know what you get.

Cheers
nathj
Jul 31 '07 #20

Sign in to post your reply or Sign up for a free account.

Similar topics

4
2930
by: JDJones | last post by:
I'm trying to write a script that will read from a text list of songs that I burned onto my CD (Albums011.txt), then write to the database text the new text made ready for inserting into a database. The entries from the Albums011.txt look like this: Wayne Newton - Wild Cool & Swingin' - 03 - But Not For Me.mp3 Wayne Newton - Wild Cool & Swingin' - 04 - Wives And Lovers.mp3 etc. and I want to manipulate it them to look in the...
2
2785
by: Amy G | last post by:
I am looking to make this code a little "nicer"... any suggestions??? I want to do a "read+" where I would be able to first read the contents of the file... then either close the file out or write to the file without appending. I want to overwrite the content. vacation_message = "blah blah blah" f_vm=open("/home/%s/.vacation.msg" %userid, 'r') lines=f_vm.readlines() f_vm.close()
0
1391
by: Joan Bos | last post by:
Hi, Is there somewhere on the Internet a description of what a .NET application config file should contain? For our application, I have to write passwords encrypted in a config file. The Enterprise Library does have an encryption method, but that method doesn't just return a value - no - it wants to write the value immediately to a config file. Except, it always gives an error, because no config file contents are ever good enough for...
22
13333
by: Jason Heyes | last post by:
Does this function need to call eof after the while-loop to be correct? bool read_file(std::string name, std::string &s) { std::ifstream in(name.c_str()); if (!in.is_open()) return false; char c; std::string str;
1
6768
by: cnu | last post by:
My program generates a log file for every event that happens in the program. So, I open the file and keep it open till the end. This is how I open the file for writing: <CODE> public CLogHandler() { this.m_fsLog = new FileStream(strTodaysLogFile, System.IO.FileMode.Append, System.IO.FileAccess.Write, System.IO.FileShare.Read); this.m_swLog = new StreamWriter(this.m_fsLog);
4
2748
by: ESPN Lover | last post by:
Below is two snippets of code from MSDN showing how to read a file. Is one way preferred over the other and why? Thanks. using System; using System.IO; class Test { public static void Main()
10
2545
by: Tibby | last post by:
I need to read/write not only text files, but binary as well. It seems like on binary files, it doesn't right the last 10% of the file. -- Thanks --- Outgoing mail is certified Virus Free. Checked by AVG anti-virus system (http://www.grisoft.com). Version: 6.0.538 / Virus Database: 333 - Release Date: 11/10/2003
8
1887
by: kepioo | last post by:
I currently have an xml input file containing lots of data. My objectiv is to write a script that reports in another xml file only the data I am interested in. Doing this is really easy using SAX. The input file is continuously updated. However, the other xml file should be updated only on request. Everytime we run the script, we track the new elements in the input file and report them in the output file.
3
2961
by: =?Utf-8?B?ZGF2aWQ=?= | last post by:
I try to follow Steve's paper to build a database, and store a small text file into SQL Server database and retrieve it later. Only difference between my table and Steve's table is that I use NTEXT datatype for the file instead of using IMAGE datatype. I can not use SqlDataReader to read the data. I need your help, Thanks. -David (1) I have a table TestFile for testing: ID int FileName navrchar(255)
2
5454
by: Kevin Ar18 | last post by:
I posted this on the forum, but nobody seems to know the solution: http://python-forum.org/py/viewtopic.php?t=5230 I have a zip file that is several GB in size, and one of the files inside of it is several GB in size. When it comes time to read the 5+GB file from inside the zip file, it fails with the following error: File "...\zipfile.py", line 491, in read bytes = self.fp.read(zinfo.compress_size) OverflowError: long it too large to...
0
10109
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10051
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9916
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8939
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6718
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5360
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5486
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4017
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2853
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.