LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
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, 03: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, 05:53 AM   #2
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,552

Rep: Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872
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, 06: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, 06:44 AM   #4
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,552

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

Original Poster
Rep: Reputation: 15
file is tab seperated..
 
Old 02-02-2005, 03:28 AM   #6
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,552

Rep: Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872
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 03:36 AM.
 
  


Reply



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 12:27 AM
Perl: Inserting new data into the middle of a file R00ts Programming 5 03-07-2005 06:48 PM
I need help with inserting data into mysql w/ PHP lostboy Programming 3 08-25-2003 12:08 PM
inserting text into a file DavidPhillips Programming 5 08-15-2003 04:53 PM
Problem Inserting MySQL data from PHP! rhuser Linux - Software 9 03-03-2003 07:56 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:03 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
Open Source Consulting | Domain Registration