473,789 Members | 1,966 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 4180
nathj
938 Recognized Expert Contributor
Hi,

That's about it, I only mentioned print_r() to aid your understanding.

Without that line in, and adjusting the output you get:

[php]
<?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);

for($j = 0; $j < count($str); $j++)
{
echo $str[$j] . "<br />";
}
$x=$x+1024;
}
$lcount=$i;
?
[/php]

This should, I beleive, output the contents of the file to screen.

Now, run it and see what you get. This should enable you to see what you still need to do.

Cheers
nathj
Aug 1 '07 #31
pradeepjain
563 Contributor
Hi,

That's about it, I only mentioned print_r() to aid your understanding.

Without that line in, and adjusting the output you get:

[php]
<?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);

for($j = 0; $j < count($str); $j++)
{
echo $str[$j] . "<br />";
}
$x=$x+1024;
}
$lcount=$i;
?
[/php]

This should, I beleive, output the contents of the file to screen.

Now, run it and see what you get. This should enable you to see what you still need to do.

Cheers
nathj
this is wht is getting printed in the o/p

"; } $x=$x+1024; } $lcount=$i; ?>
"); $i = $i + 1; } ?>


this is the code tht i am using


[PHP] <?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);

for($j = 0; $j < count($str); $j++)
{
echo $str[$j] . "<br />";//lets say $str[0] contains "name" and $str[1] contains "pradeep" .but as we come to 2nd line of the file and this for loop the str[0] wll contain "place" and and str[1] wll contain "bangalore" .this wll be a prob in html calling ..

}
$x=$x+1024;
}
$lcount=$i;
?>
[/PHP]
[HTML]<html>
<head>
</head>
<body>

<FORM METHOD=post ACTION="test1.p hp">

<?
print $lcount;
for($i=0;$i <= $lcount ;$i++){

print("<input name=" . $str[$i] ." type=text value=" . $str[$i+1] ." >");


$i = $i + 1;
}

?>
<INPUT TYPE=SUBMIT>
</form>
</body>
</html>
[/HTML]

ok how to call the in the html separetely i.e name and pradeep..
ie the it shld be lke
the html page must be lke this

name:<input type="text" value="pradeep>
place:<input type="text" value="bangalor e>

see line 11
Aug 1 '07 #32
nathj
938 Recognized Expert Contributor
Just a suggestion but you could format the file differently. So that you have || to separate between full items such as the name and place combination and then && to separate between sub entities. This would give you file like

name:pradeep&&l ocation:bangalo re||name:smith& &location:somew here

Without the need for the low level file functions, as you have no line breaks you do:
[php]
<?php

$lcFileContents = file(_FileName_ );
$laContents = explode("||", $lcFileContents ); // element 0 = name:pradeep&&l ocation:bangalo re

$lnArrayLength = count($laConten ts);
for(i=0; i<=$lnArrayLeng th; $i++)
{
$lcMainSet = $laContents[$i];
$laSubArray = explode["&&", $lcMainSet]; //elemnt 0 = name:pradeep

// nest another appropriate loop and youre away
}
?>
[/php]

Hopefully get the idea, the use of nested loops can get messy so be warned and write useful comments in the code to help sign post the process to anyone else who reads it.

Have a play and let me know how you get on.

Cheers
nathj
Aug 1 '07 #33
pradeepjain
563 Contributor
Just a suggestion but you could format the file differently. So that you have || to separate between full items such as the name and place combination and then && to separate between sub entities. This would give you file like

name:pradeep&&l ocation:bangalo re||name:smith& &location:somew here

Without the need for the low level file functions, as you have no line breaks you do:
[php]
<?php

$lcFileContents = file(_FileName_ );
$laContents = explode("||", $lcFileContents ); // element 0 = name:pradeep&&l ocation:bangalo re

$lnArrayLength = count($laConten ts);
for(i=0; i<=$lnArrayLeng th; $i++)
{
$lcMainSet = $laContents[$i];
$laSubArray = explode["&&", $lcMainSet]; //elemnt 0 = name:pradeep

// nest another appropriate loop and youre away
}
?>
[/php]

Hopefully get the idea, the use of nested loops can get messy so be warned and write useful comments in the code to help sign post the process to anyone else who reads it.

Have a play and let me know how you get on.

Cheers
nathj


the idea got rejected from my boss...the file structure is same.
name:pradeep
place:bangalore
addess:bsk
Aug 1 '07 #34
pradeepjain
563 Contributor
Just a suggestion but you could format the file differently. So that you have || to separate between full items such as the name and place combination and then && to separate between sub entities. This would give you file like

name:pradeep&&l ocation:bangalo re||name:smith& &location:somew here

Without the need for the low level file functions, as you have no line breaks you do:
[php]
<?php

$lcFileContents = file(_FileName_ );
$laContents = explode("||", $lcFileContents ); // element 0 = name:pradeep&&l ocation:bangalo re

$lnArrayLength = count($laConten ts);
for(i=0; i<=$lnArrayLeng th; $i++)
{
$lcMainSet = $laContents[$i];
$laSubArray = explode["&&", $lcMainSet]; //elemnt 0 = name:pradeep

// nest another appropriate loop and youre away
}
?>
[/php]

Hopefully get the idea, the use of nested loops can get messy so be warned and write useful comments in the code to help sign post the process to anyone else who reads it.

Have a play and let me know how you get on.

Cheers
nathj

ANY OTHER SUGGESTION AS HOW TO CONTINUE WITH THE OLD FILE STRUCTURE ITSELF....
Aug 1 '07 #35
nathj
938 Recognized Expert Contributor
ANY OTHER SUGGESTION AS HOW TO CONTINUE WITH THE OLD FILE STRUCTURE ITSELF....
First, please don't shout at me. You are not paying me, I am helping you because I am kind that way.

Second, you need to take the code you've got and try different things. It may be that you have to hard code some rules into the php. I know it sounds nasty but it's not that bad.

Please stop panicing and this project, take a break and then play around with what you've got. My mantra is keep trying and keep learning. That's exactly what you need to do.

I've provided you psuedo code and code samples. It's about time you did something for yourself. As you can see my patience is running out.

nathj
Aug 1 '07 #36
pradeepjain
563 Contributor
First, please don't shout at me. You are not paying me, I am helping you because I am kind that way.

Second, you need to take the code you've got and try different things. It may be that you have to hard code some rules into the php. I know it sounds nasty but it's not that bad.

Please stop panicing and this project, take a break and then play around with what you've got. My mantra is keep trying and keep learning. That's exactly what you need to do.

I've provided you psuedo code and code samples. It's about time you did something for yourself. As you can see my patience is running out.

nathj


hey really srry if u felt lke i was shouting on u...
hey look at this code .i hve done it properly but its still not working can u try this and tell me


[HTML] <html>
<head>
</head>
<body>


<?php

$filehandle = fopen("/var/www/html/welcome.txt","r ");

$line=true;

echo '<form method=post action=action.p hp>';

while($filehand le && $line){

$line = fgets($filehand le);

$str = explode(':', $line);

echo '<input name="'.$str[0].'" value="'.$str[1].'"><br />';
}

echo '<input type=submit value= submit>';
echo '</form>'
?>
</body>
</html>
[/HTML]
Aug 1 '07 #37
nathj
938 Recognized Expert Contributor
hey really srry if u felt lke i was shouting on u...
hey look at this code .i hve done it properly but its still not working can u try this and tell me


[HTML] <html>
<head>
</head>
<body>


<?php

$filehandle = fopen("/var/www/html/welcome.txt","r ");

$line=true;

echo '<form method=post action=action.p hp>';

while($filehand le && $line){

$line = fgets($filehand le);

$str = explode(':', $line);

echo '<input name="'.$str[0].'" value="'.$str[1].'"><br />';
}

echo '<input type=submit value= submit>';
echo '</form>'
?>
</body>
</html>
[/HTML]

Apology accepted.

The final echo line doesn't have a semi-colon on the end.

When yo usay it's nt working what exactly are you getting on screen and in the generated source code?

Also turn on the error reporting in php and see what it tells you, this may give you a clue as to what is going on.

All in all it looks like a nice solution.

Cheers
nathj
Aug 1 '07 #38
pradeepjain
563 Contributor
Apology accepted.

The final echo line doesn't have a semi-colon on the end.

When yo usay it's nt working what exactly are you getting on screen and in the generated source code?

Also turn on the error reporting in php and see what it tells you, this may give you a clue as to what is going on.

All in all it looks like a nice solution.

Cheers
nathj
its printing the echo commands at it is lke its giving text box and inside it its giving $str[1]..i am not able to properly explain whts ..so i asked u to see it once by trying it.

hey i added this file to /var/www/html/ and then within html i created welcome.txt
and the accessed it in webbrowser by http://localhost/i.html(html filename)..is this process correct..
Aug 1 '07 #39
nathj
938 Recognized Expert Contributor
Hi pradeepjain,

I have tried this with the following file:

Expand|Select|Wrap|Line Numbers
  1. name:micahel
  2. location:Newcastle
  3. name:Steven
  4. location:Liverpool
  5. name:Jamie
  6. location:Liverpool
  7.  
Using the following php
[php]
<?php
$filehandle = fopen("test.txt ","r");
$line=true;
echo '<form method=post action=action.p hp>';
while($filehand le && $line)
{
$line = fgets($filehand le);
if (!empty($line))
{
$str = explode(':', $line);
echo $str[0] . ': <input type="text" name="'.$str[0].'" value="'.$str[1].'"><br />';
}
}

echo '<input type=submit value= submit>';
echo '</form>'
?>
[/php]

This gives me a page with 6 text boxes and labels on.

The text file is currently in the same directory as the php file but that shouldn't make any difference.

Hopefully you will get this finished in time for Saturday and will have learnt a bit more about PHP.

Please take some time after this project to read some php tutorials. This will give you a better understanding of the basics and help you to get better at it very quickly.

Cheers
nathj
Aug 2 '07 #40

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
2787
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
13335
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
6770
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
1889
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
5458
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
9656
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9499
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10374
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10121
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
8995
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
6750
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
5539
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4076
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
2898
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.