有一个TXT文件,大概300多MB,想要PHP读取出来,然后分条插入到数据库,应该怎么做呢,TXT文件格式大概是这样的。应该怎么样做呢
wangli # 20100203 # 李四
liwsi # 2015544 # xiaofen
wangli # 20100203 # 李四
liwsi # 2015544 # xiaofen
wangli # 20100203 # 李四
liwsi # 2015544 # xiaofen
wangli # 20100203 # 李四
liwsi # 2015544 # xiaofen
wangli # 20100203 # 李四
liwsi # 2015544 # xiaofen
wangli # 20100203 # 李四
liwsi # 2015544 # xiaofen
这是我自己写的,但是只是可以读取出并且显示除了,不知道怎么插入数据库,而且TXT文件里面本来是有换行的,但是PHP读取出来都么有换行,还有一个问题就是如果用fread($data,1000)这个地方的是1000是指读取1000个字符,那如果需要一行一行的读取呢。现在这个TXT是一行一行这样的,如果说1000个字符这样子,那有可能最后只是读取到半行
<?php$data='11.txt';$files=fopen($data,"r");$res=fread($files,100000);echo $res.'<br>';
回复讨论(解决方案)
$data = '11.txt';$fp = fopen($data, 'r');while($r = fgets($fp)) { $t = join("','", explode(' # ', $r)); $sql = "insert into 表 values ('$t')"; //.... 执行sql}
不过对于 mysql 似乎应该用 LOAD DATA INFILE 指令来完成
PHP code?1234567$data = '11.txt';$fp = fopen($data, 'r');while($r = fgets($fp)) { $t = join("','", explode(' # ', $r)); $sql = "insert into 表 values ('$t')"; //.... 执行sql}
不过对于 mysq……
我按照你的这个,然后打开这个页面,直接不显示,空白的~
$data = '11.txt';$fp = fopen($data, 'r');while($r = fgets($fp)) { echo $t = join("','", explode(' # ', $r)); //$sql = "insert into 表 values ('$t')"; //.... 执行sql}
# 是不用插入到数据库里面的,另外如果这个文件很大,怎么分割呢
怎么样输出到PHP一行一行来显示呢,TXT默认换行是\n,这样的,PHP怎么匹配出来
你的数据不是按行按列(" # ")做值得吗?
只有你提供正确的数据和需求,才有可能得到可行的处理方案
$filename="11.txt";$fps=fopen($filename,'r');while(!feof($fps)){$ceshi=fgets($fps);echo $ceshi."<br>";}fclose($fps);
谢谢版主,我是按照这样的,然后浏览器输出以后就是和TXT文本一样显示出来了,但是如果要插入到数据库里面应该怎么做呢
我数据库里面有四个字段,id,name,time,bak
第一个是ID是auto_increment
其余三个字段都是varchar
现在TXT文件是格式是
lisi # 121015 # 3900
xiaomao # 130205 #2500
lisa # 1258 # ok
我想插入到数据里面以后的数据是这样的
id name time bak
1 lisi 121015 3900
2 xiaomao 130205 2500
3 lisa 1258 ok
我没有弄错,列分隔符是“ # ”,即 # 号两边各有一个空格(真难为你了,不嫌烦吗?)
补上字段列表,为
$t = join("','", explode(' # ', $r));
$sql = "insert into 表 ( name, time, bak) values ('$t')";

Long URLs, often cluttered with keywords and tracking parameters, can deter visitors. A URL shortening script offers a solution, creating concise links ideal for social media and other platforms. These scripts are valuable for individual websites a

Laravel simplifies handling temporary session data using its intuitive flash methods. This is perfect for displaying brief messages, alerts, or notifications within your application. Data persists only for the subsequent request by default: $request-

This is the second and final part of the series on building a React application with a Laravel back-end. In the first part of the series, we created a RESTful API using Laravel for a basic product-listing application. In this tutorial, we will be dev

Laravel provides concise HTTP response simulation syntax, simplifying HTTP interaction testing. This approach significantly reduces code redundancy while making your test simulation more intuitive. The basic implementation provides a variety of response type shortcuts: use Illuminate\Support\Facades\Http; Http::fake([ 'google.com' => 'Hello World', 'github.com' => ['foo' => 'bar'], 'forge.laravel.com' =>

The PHP Client URL (cURL) extension is a powerful tool for developers, enabling seamless interaction with remote servers and REST APIs. By leveraging libcurl, a well-respected multi-protocol file transfer library, PHP cURL facilitates efficient execution of various network protocols, including HTTP, HTTPS, and FTP. This extension offers granular control over HTTP requests, supports multiple concurrent operations, and provides built-in security features.

Do you want to provide real-time, instant solutions to your customers' most pressing problems? Live chat lets you have real-time conversations with customers and resolve their problems instantly. It allows you to provide faster service to your custom

The 2025 PHP Landscape Survey investigates current PHP development trends. It explores framework usage, deployment methods, and challenges, aiming to provide insights for developers and businesses. The survey anticipates growth in modern PHP versio

In this article, we're going to explore the notification system in the Laravel web framework. The notification system in Laravel allows you to send notifications to users over different channels. Today, we'll discuss how you can send notifications ov


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

Dreamweaver Mac version
Visual web development tools

Notepad++7.3.1
Easy-to-use and free code editor

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft
