PHP reads TXT files and inserts them into the database

Source: Internet
Author: User
PHP reads the TXT file and inserts it into the database. there is a TXT file, which is about 300 MB or more. what should I do if I want PHP to read it and insert it into the database in different entries? the TXT file format is like this. What should I do?

Wangli #20100203 # Li Si
Liwsi #2015544 # xiaofen
Wangli #20100203 # Li Si
Liwsi #2015544 # xiaofen
Wangli #20100203 # Li Si
Liwsi #2015544 # xiaofen
Wangli #20100203 # Li Si
Liwsi #2015544 # xiaofen
Wangli #20100203 # Li Si
Liwsi #2015544 # xiaofen
Wangli #20100203 # Li Si
Liwsi #2015544 # xiaofen


I wrote this by myself, but it can only be read and retrieved and displayed, except that I don't know how to insert the database, and there is a line break in the TXT file, but there is a line break when PHP reads it, another problem is that if fread ($ data, 1000) is used, where 1000 refers to reading 1000 characters, what if one row and one row are needed. Now this TXT is like a row. if the TXT contains 1000 characters, it is possible that it only reads half a row.

 ';


Reply to discussion (solution)

$ Data = '11.txt '; $ fp = fopen ($ data, 'r'); while ($ r = fgets ($ fp) {$ t = join ("', '", explode (' # ', $ r); $ SQL =" insert into table values (' $ t ')";//.... execute SQL}


However, it seems that mysql should be completed using the load data infile command.

PHP code? 1234567 $ data = '11.txt '; $ fp = fopen ($ data, 'r'); while ($ r = fgets ($ fp) {$ t = join ("', '", explode (' # ', $ r); $ SQL =" insert into table values (' $ t ')";//.... execute SQL}

However, for mysq ......

I follow your instructions and open this page without displaying it. it is blank ~

$ Data = '11.txt '; $ fp = fopen ($ data, 'r'); while ($ r = fgets ($ fp) {echo $ t = join ("', '", explode (' # ', $ r); // $ SQL =" insert into table values (' $ t ')";//.... execute SQL}




# There is no need to insert the file into the database. In addition, if the file is large, how can we split it?

How to output a line to PHP for display? by default, the TXT line feed is \ n. how can PHP match this line?

Isn't your data worthwhile by column ("#") by row?
Only when you provide the correct data and requirements can you get a feasible solution.

$filename="11.txt";$fps=fopen($filename,'r');while(!feof($fps)){$ceshi=fgets($fps);echo $ceshi."
";}fclose($fps);


Thanks to the moderator. I used this method, and the browser output will be displayed in the same way as the TXT text. but what should I do if I want to insert it into the database?

My database contains four fields: id, name, time, bak

The first one is that the ID is auto_increment.
All three other fields are varchar.
The TXT file is in the format
Lisi #121015 #3900
Xiaomao #130205 #2500
Lisa #1258 # OK
I want to insert the data into the data.
Id name time bak
1 lisi 121015 3900
2 xiaomao 130205 2500
3 lisa 1258 OK




I didn't make a mistake. the column separator is "#", that is, there is a space on both sides of # (it's really hard for you, don't you bother ?)
Fill in the field list, which is
$ T = join ("','", explode ('#', $ r ));
$ SQL = "insert into table (name, time, bak) values ('$ t ')";

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.