Home Backend Development PHP Tutorial PHP读取硕大无比文件的实例代码

PHP读取硕大无比文件的实例代码

Jun 13, 2016 pm 12:23 PM
content tag

PHP读取超大文件的实例代码

数据量大带来的问题就是单个文件很大,能够打开这个文件相当不容易,记事本就不要指望了,果断死机
 

去年年底的各种网站帐号信息的数据库泄漏,很是给力啊,趁机也下载了几个数据库,准备学学数据分析家来分析一下这些帐号信息。虽然这些数据信息都已经被“整理”过的,不过自己拿来学习也挺有用的,毕竟有这么大的数据量。

数据量大带来的问题就是单个文件很大,能够打开这个文件相当不容易,记事本就不要指望了,果断死机。用MSSQL的客户端也打不开这么大的SQL文件,直接报内存不足,原因据说是MSSQL在读取数据的时候,是一次性地将读取到的数据放在内存中,如果数据量过大,而内存不足,则会直接导致系统瘫掉。

Navicat Premium
这儿推荐一个软件Navicat Premium,相当给力啊,几百兆的SQL文件轻松就打开了,一点都不卡。而且这个客户端软件支持MSSQL、MYSQL、Oracle……等等各种数据库的连接,其它的很多功能就自己慢慢研究了。

虽然用Navicat可以打开CSDN这个274MB的SQL文件,但是内容却是没意义的,而且也不方便对这些帐号信息进行查询、分类、统计等等操作。唯一的方法就是把这些数据一条一条地读取出来,然后分拆每条记录的不同片段,再将这些片段以数据字段的格式存入数据库,这样就可以方便以后的使用了。

使用PHP读取超大文件
PHP有很多种文件读取的方式,根据目标文件的不同,采取更合适的方法,可有效地提高执行效率。由于CSDN数据库文件很大,所以我们尽量不在短时间内全都读取出来,毕竟每读取一条数据还要对其分拆和写入操作。那么比较合适的方式就是对文件进行分区域地读取,通过使用PHP的fseek和fread相结合,即可做到随意读取文件中的某一部份数据,下面是实例代码:

代码如下:function readBigFile($filename, $count = 20, $tag = "\r\n") {$content = "";//最终内容$current = "";//当前读取内容寄存$step= 1;//每次走多少字符$tagLen = strlen($tag);$start = 0;//起始位置$i = 0;//计数器$handle = fopen($filename,'r+');//读写模式打开文件,指针指向文件起始位置while($i < $count && !feof($handle)) {fseek($handle, $start, SEEK_SET);//指针设置在文件开头$current = fread($handle,$step);//读取文件$content .= $current;//组合字符串$start += $step;//依据步长向前移动//依据分隔符的长度截取字符串最后免得几个字符$substrTag = substr($content, -$tagLen);if ($substrTag == $tag) { //判断是否为判断是否是换行或其他分隔符$i++;$content .= "<br />";}}//关闭文件fclose($handle);//返回结果return $content;}$filename = "csdn.sql";//需要读取的文件$tag = "\n";//行分隔符 注意这里必须用双引号$count = 100;//读取行数$data = readBigFile($filename,$count,$tag);echo $data; 
Copy after login

 


关于函数传入的变量$tag的值,根据系统不一样,传入的值也是有区别的:Windows用”\r\n”,linux/unix用”\n”,Mac OS用”\r”。

程序执行的大概流程:先定义读取文件的一些基础变量,然后打开文件,将指针定位在文件的指定位置,并读取指定大小的内容。每读取一次将内容存储在变量中,直到达到读取要求的行数或文件结束。

绝不要假定程序中的一切都将按计划运行。

根据上面的代码,虽然能够得到文件中指定位置、指定大小的数据,但这整个过程只执行了一次,并不能得到所有的数据。其实要得到所有的数据,可以在这个循环的外层再添加判断文件是否结束的循环,但这很浪费系统资源,甚至由于文件过大一直没法读完而导致PHP执行超时。另一种方法就是记录并存储上次读取数据后指针所在的位置,然后再次执行该循环的时候,将指针定位在上次结束的位置,这样就不存在一次循环要把文件从头读到尾的情况。

其实CSDN这个数据库我到现在都还没有导入数据库,因为当时泄漏后没几天CNBETA上就有一个分析了,呵呵,动作太快了。当看到别人已经做了这个事之后,自动就没有多少动力来做了,不过为了学习,还是要抽时间把这个事完成了。

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1659
14
PHP Tutorial
1258
29
C# Tutorial
1232
24
How does session hijacking work and how can you mitigate it in PHP? How does session hijacking work and how can you mitigate it in PHP? Apr 06, 2025 am 12:02 AM

Session hijacking can be achieved through the following steps: 1. Obtain the session ID, 2. Use the session ID, 3. Keep the session active. The methods to prevent session hijacking in PHP include: 1. Use the session_regenerate_id() function to regenerate the session ID, 2. Store session data through the database, 3. Ensure that all session data is transmitted through HTTPS.

Explain different error types in PHP (Notice, Warning, Fatal Error, Parse Error). Explain different error types in PHP (Notice, Warning, Fatal Error, Parse Error). Apr 08, 2025 am 12:03 AM

There are four main error types in PHP: 1.Notice: the slightest, will not interrupt the program, such as accessing undefined variables; 2. Warning: serious than Notice, will not terminate the program, such as containing no files; 3. FatalError: the most serious, will terminate the program, such as calling no function; 4. ParseError: syntax error, will prevent the program from being executed, such as forgetting to add the end tag.

PHP and Python: Comparing Two Popular Programming Languages PHP and Python: Comparing Two Popular Programming Languages Apr 14, 2025 am 12:13 AM

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

What are HTTP request methods (GET, POST, PUT, DELETE, etc.) and when should each be used? What are HTTP request methods (GET, POST, PUT, DELETE, etc.) and when should each be used? Apr 09, 2025 am 12:09 AM

HTTP request methods include GET, POST, PUT and DELETE, which are used to obtain, submit, update and delete resources respectively. 1. The GET method is used to obtain resources and is suitable for read operations. 2. The POST method is used to submit data and is often used to create new resources. 3. The PUT method is used to update resources and is suitable for complete updates. 4. The DELETE method is used to delete resources and is suitable for deletion operations.

Explain secure password hashing in PHP (e.g., password_hash, password_verify). Why not use MD5 or SHA1? Explain secure password hashing in PHP (e.g., password_hash, password_verify). Why not use MD5 or SHA1? Apr 17, 2025 am 12:06 AM

In PHP, password_hash and password_verify functions should be used to implement secure password hashing, and MD5 or SHA1 should not be used. 1) password_hash generates a hash containing salt values ​​to enhance security. 2) Password_verify verify password and ensure security by comparing hash values. 3) MD5 and SHA1 are vulnerable and lack salt values, and are not suitable for modern password security.

PHP: A Key Language for Web Development PHP: A Key Language for Web Development Apr 13, 2025 am 12:08 AM

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

Explain Arrow Functions (short closures) introduced in PHP 7.4. Explain Arrow Functions (short closures) introduced in PHP 7.4. Apr 06, 2025 am 12:01 AM

The arrow function was introduced in PHP7.4 and is a simplified form of short closures. 1) They are defined using the => operator, omitting function and use keywords. 2) The arrow function automatically captures the current scope variable without the use keyword. 3) They are often used in callback functions and short calculations to improve code simplicity and readability.

PHP in Action: Real-World Examples and Applications PHP in Action: Real-World Examples and Applications Apr 14, 2025 am 12:19 AM

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

See all articles