473,775 Members | 2,404 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
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

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] . ' />' ;

$i = $i + 1;
}

echo '<input type="submit" value="Complete "/>';

echo '</form>';

?>



</form>
</body>
</html>

this is wht i hve used as code..but still not working...

error:'; for ($i=0; $i <= $lcount; $i++) { echo '' ; $i = $i + 1; } echo ''; echo ''; ?>


i thnk there is a problem in 2 arrays...is th 1st php code proper
Jul 31 '07 #21
nathj
938 Recognized Expert Contributor
What information is in the two arrays you have? It may be that we need to adjust the code to include quote marks if the data in the array contains spaces.


Also are you getting an error message - what is the message exactly?

Cheers
nathh
Jul 31 '07 #22
pradeepjain
563 Contributor
What information is in the two arrays you have? It may be that we need to adjust the code to include quote marks if the data in the array contains spaces.


Also are you getting an error message - what is the message exactly?

Cheers
nathh

the file contains
name:pradeep
palce:blore

i am taking the 1st line into data and then using explode so as 2 split into 2 arrays say name and pradeep..and storing them in name[$i] and value[$i]
then i am moving to next line wit help of $x+1024 and then splitting the 2nd line also..
then using the variables in the html form..
there is no proper error ..but its displaying jst tht wht i said in last msg.
Jul 31 '07 #23
nathj
938 Recognized Expert Contributor
the file contains
name:pradeep
palce:blore

i am taking the 1st line into data and then using explode so as 2 split into 2 arrays say name and pradeep..
explode() will generate one array:
[PHP]
<?php
$lcFileContent = file(_FileName_ );
$laTest = explode(":", $lcFileContent) ;
print_r($laTest );
[/PHP]

This single array will then contain 'name' and 'pradeep'. The code abpve will print the array and you will be able to see what is in it and how best to manipulate.

Cheers
nathj
Jul 31 '07 #24
pradeepjain
563 Contributor
explode() will generate one array:
[PHP]
<?php
$lcFileContent = file(_FileName_ );
$laTest = explode(":", $lcFileContent) ;
print_r($laTest );
[/PHP]

This single array will then contain 'name' and 'pradeep'. The code abpve will print the array and you will be able to see what is in it and how best to manipulate.

Cheers
nathj

but this is wht in found i php manual

If you want to split a price (float) into pounds and pence.

or dollors and cents etc etc.

$price = "6.20";

$split = explode(".", $price);
$pound = $split[0]; // piece1
$pence = $split[1]; // piece2

echo "&pound $pound . $pence\n";

seeing this only i used tht...

hey i am not finding a way wht to do..if i dont show the code before SATURDAY i wll kicked off frm the job..,
Jul 31 '07 #25
nathj
938 Recognized Expert Contributor
but this is wht in found i php manual

If you want to split a price (float) into pounds and pence.

or dollors and cents etc etc.

$price = "6.20";

$split = explode(".", $price);
$pound = $split[0]; // piece1
$pence = $split[1]; // piece2

echo "&pound $pound . $pence\n";

seeing this only i used tht...

hey i am not finding a way wht to do..if i dont show the code before SATURDAY i wll kicked off frm the job..,
First of all please use CODE tags, it makes reading your post much easier!

Second, you may be in a panic but I'm just trying to help and perhaps you need to take a breather before reading this and come back to it with a clearer mind. I know I need to do that sometimes.

The code sample you posted
[PHP]
$price = "6.20";

$split = explode(".", $price);
$pound = $split[0]; // piece1 - "6"
$pence = $split[1]; // piece2 - "20"

echo "&pound $pound . $pence\n";
[/PHP]
Simply takes string and splits on the occurrence of '.' into one array called $split. This array has numeric keys, starting at 0 and can be referenced accoridingly.

In your case all that is different is the source of the original string - you want it to come from a file.

What you need to do, is what you were doing earlier (I showed the file() function and print_r() to help you understand what happens with the xplode function).

Open the file and loop through one line at a time.
For each line create a variable of the line.
Explode the variableit into an array
Loop through this array (this is now a nested loop)
This array will, according to your data structure, contain a pair of values, do what you want with these. Note; as its a numeric array it is simple to loop with a for loop based on the count() of the array - remember arrasy start counting at 0.

This is the psuedo code for you. Just write it up. I've given enough help now on this. I do have a job to do myself you know, with my own deadlines to meet - you're not the only one under pressure.

If you write the code, as outlined, and need some help post it back here with lots of information and use the CODE or PHP tags to format it.

nathj
Jul 31 '07 #26
ak1dnar
1,584 Recognized Expert Top Contributor
pradeepjain,

Please read the Forum rules and Posting guidelines while you using the forums.And please pay your kind attention to these;
Jul 31 '07 #27
pradeepjain
563 Contributor
First of all please use CODE tags, it makes reading your post much easier!

Second, you may be in a panic but I'm just trying to help and perhaps you need to take a breather before reading this and come back to it with a clearer mind. I know I need to do that sometimes.

The code sample you posted
[PHP]
$price = "6.20";

$split = explode(".", $price);
$pound = $split[0]; // piece1 - "6"
$pence = $split[1]; // piece2 - "20"

echo "&pound $pound . $pence\n";
[/PHP]
Simply takes string and splits on the occurrence of '.' into one array called $split. This array has numeric keys, starting at 0 and can be referenced accoridingly.

In your case all that is different is the source of the original string - you want it to come from a file.

What you need to do, is what you were doing earlier (I showed the file() function and print_r() to help you understand what happens with the xplode function).

Open the file and loop through one line at a time.
For each line create a variable of the line.
Explode the variableit into an array
Loop through this array (this is now a nested loop)
This array will, according to your data structure, contain a pair of values, do what you want with these. Note; as its a numeric array it is simple to loop with a for loop based on the count() of the array - remember arrasy start counting at 0.

This is the psuedo code for you. Just write it up. I've given enough help now on this. I do have a job to do myself you know, with my own deadlines to meet - you're not the only one under pressure.

If you write the code, as outlined, and need some help post it back here with lots of information and use the CODE or PHP tags to format it.

nathj


[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);

print_r($str); //ok as u said this wll contain name pradeep

//how to use 2nd loop..i did not get u on this.


$x=$x+1024;
$i=$i+1;
}
$lcount=$i;
?>[/PHP]
Jul 31 '07 #28
pradeepjain
563 Contributor
If u cld show how to use 2nd loop and how to store "name" and "pradeep" in 2 diff variables...i wll b graetful to u..
Aug 1 '07 #29
pradeepjain
563 Contributor
First of all please use CODE tags, it makes reading your post much easier!

Second, you may be in a panic but I'm just trying to help and perhaps you need to take a breather before reading this and come back to it with a clearer mind. I know I need to do that sometimes.

The code sample you posted
[PHP]
$price = "6.20";

$split = explode(".", $price);
$pound = $split[0]; // piece1 - "6"
$pence = $split[1]; // piece2 - "20"

echo "&pound $pound . $pence\n";
[/PHP]
Simply takes string and splits on the occurrence of '.' into one array called $split. This array has numeric keys, starting at 0 and can be referenced accoridingly.

In your case all that is different is the source of the original string - you want it to come from a file.

What you need to do, is what you were doing earlier (I showed the file() function and print_r() to help you understand what happens with the xplode function).

Open the file and loop through one line at a time.
For each line create a variable of the line.
Explode the variableit into an array
Loop through this array (this is now a nested loop)
This array will, according to your data structure, contain a pair of values, do what you want with these. Note; as its a numeric array it is simple to loop with a for loop based on the count() of the array - remember arrasy start counting at 0.

This is the psuedo code for you. Just write it up. I've given enough help now on this. I do have a job to do myself you know, with my own deadlines to meet - you're not the only one under pressure.

If you write the code, as outlined, and need some help post it back here with lots of information and use the CODE or PHP tags to format it.

nathj

I HVE TRIED THIS IS THIS WHT U SAID TO DO

[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);
print_r($str);
for($j = 0; $j < count($str); $j++){
echo "str #$j = $str[$j]<br />";
}
$x=$x+1024;
}
$lcount=$i;
?>
[/PHP]
Aug 1 '07 #30

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
9622
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
10270
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...
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
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

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.