LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 02-01-2005, 04:42 AM   #1
suchi_s
Member
 
Registered: May 2004
Posts: 133

Rep: Reputation: 15
inserting the data thru php in a text file


insert data in a text file through php
how ?
code?
 
Old 02-01-2005, 06:53 AM   #2
keefaz
Senior Member
 
Registered: Mar 2004
Distribution: Slackware
Posts: 4,617

Rep: Reputation: 136Reputation: 136
Could you ellaborate with insert data ?

The basic code to write in a text file could be :
PHP Code:
$file "/path/to/file";
$fd fopen($file"a"); // a for append, append text to file
if(!$fd) { die("Could not open $file to write in"); }
fwrite($fd"something");
fclose($fd); 
 
Old 02-01-2005, 07:28 AM   #3
suchi_s
Member
 
Registered: May 2004
Posts: 133

Original Poster
Rep: Reputation: 15
but how to update the particular record in a file through file or a particular field in a record
 
Old 02-01-2005, 07:44 AM   #4
keefaz
Senior Member
 
Registered: Mar 2004
Distribution: Slackware
Posts: 4,617

Rep: Reputation: 136Reputation: 136
That depends on how you format your file, is it csv or is it like "field = value" ?
 
Old 02-01-2005, 11:27 PM   #5
suchi_s
Member
 
Registered: May 2004
Posts: 133

Original Poster
Rep: Reputation: 15
file is tab seperated..
 
Old 02-02-2005, 04:28 AM   #6
keefaz
Senior Member
 
Registered: Mar 2004
Distribution: Slackware
Posts: 4,617

Rep: Reputation: 136Reputation: 136
So I think the function fgetcsv() will be usefull here. Say you have a file :
Code:
one     two     three
cat     dog     bird
man     woman   child
And you want to update "bird" (which is on the 3rd field column) and replace it
with "falcon"
PHP Code:
// your tab separated file
$file "/path/to/file";
// a temp file
$tmp tempnam("/tmp""tmpfile");

// open tab separated file
$fd fopen($file"r");
if(!
$fd) {
    echo 
"Could not open $file in read only mode\n";
    exit(
1);
}

// open tmp file to write in
$fw fopen($tmp"w");
if(!
$fw) {
    echo 
"Could not open the temp file in write mode\n";
    exit(
1);
}
while ((
$line fgetcsv($fd1000"\t")) !== FALSE) {
    if(
$line[2] == "bird") { $line[2] = "falcon"; }
    
$text implode("\t"$line)."\n";
    
fwrite($fw$text);
}

fclose($fd);
fclose($fw);

// now just copy temp file to tab separated file (need write permission)
copy($tmp$file) or die("Could not copy tmp to $file"); 
unlink($tmp); 

Last edited by keefaz; 02-02-2005 at 04:36 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem inserting data into a mysql table using PHP Rockgod2099 Programming 13 08-03-2005 01:27 AM
Perl: Inserting new data into the middle of a file R00ts Programming 5 03-07-2005 07:48 PM
I need help with inserting data into mysql w/ PHP lostboy Programming 3 08-25-2003 01:08 PM
inserting text into a file DavidPhillips Programming 5 08-15-2003 05:53 PM
Problem Inserting MySQL data from PHP! rhuser Linux - Software 9 03-03-2003 08:56 PM


All times are GMT -5. The time now is 04:48 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration