HI
I`m trying to read data from a text file and then store it into the database so i can save the content of the text file daily, A kind of backup:
to read the file i`m using
$filename = "backup.txt";
$fp = fopen ($filename, "r" ) or die ("could not find file");
while ( ! feof( $fp ) ){
$line = fgets ($fp, 1024 );
print "$line<BR>";
}
this prints out the file but how do i convert this into a single string like
$data= nl2br($line);
so i can add it to the database
Thanks
Text file to database
Moderator: General Moderators
- daven
- Forum Contributor
- Posts: 332
- Joined: Tue Dec 17, 2002 1:29 pm
- Location: Gaithersburg, MD
- Contact:
I tend to use tab/carriage return delimited text files and the SQL "LOAD DATA" call.
States DB table:
states.txt
State_Data.php:
States DB table:
Code: Select all
Abr VARCHAR(2) NULL,
State VARCHAR(30) NULLCode: Select all
AK AK-Alaska
AL AL-Alabama
AR AR-Arkansas
AZ AZ-Arizona
CA CA-California
CO CO-Colorado
CT CT-Conneticut
DC DC-District of Columbia
DE DE-Delaware
FL FL-Florida
GA GA-Georgia
HI HI-HawaiiState_Data.php:
Code: Select all
<?php
$qry_state_data="LOAD DATA LOCAL INFILE "./states.txt" INTO TABLE States";
mysql_query($qry_state_data,$conn) or die($qry_state_data);
?>to convert to a single string use the .= operator to append to a string. Eg.
is the same as
So you'd use :
the you could use nl2br($line);
but you might as well change the newlines to breaks by instesd of using
use
Code: Select all
$str = "this";
$str .= " is crazy";Code: Select all
$str = "this is crazy"Code: Select all
$line = "";
while ( ! feof( $fp ) ){
$line .= fgets ($fp, 1024 );
$line .= \n";
}but you might as well change the newlines to breaks by instesd of using
Code: Select all
$line .= \n";Code: Select all
$line .= <BR>\n";Code: Select all
$filename = "backup.txt";
$fp = fopen ($filename, "r" ) or die ("could not find file");
$file = fread ($fp, filesize($filename) );
print "$file";