php+ajax导入大数据时产生的问题处理_php实例
遇到的问题就从先到后的一一说吧。
问题1 按照我最初的想法,先上传文件再读取文件。这里问题就来了,当文件较大的时候上传较慢,导致客户看到的操作一直处于等待状态,不人性化。
处理办法:我是这样做的,大神有更好的办法,求介绍。我先把文件上传上去,然后把文件存到一个特定的文件夹就叫 import吧 ,然后返回一个这个文件名字。这样就确保了文件是上传成功的。并且我可以在他返回名字的这一步用js 给客户一个提示。然后就是ajax去请求php读取文件,插入数据库。可是问题来了。
问题2 当我用ajax去请求php读取文件并插入数据库的时候,遇到一个问题,就是ajax请求总是在1min的时候,断掉。我一想 ,这应该是php的最大执行时间max_execution_time的原因吧,结果我修改为300秒。还是这样,那我就认为会不会是apache的 最大get时间max_input_time呢,我就在代码加一个 ini_set 结果,用ini_get 查看max_input_time,用ini_set设置无效,还是60秒,在网上查了很多资料,还是不知道为啥。有大神知道的,请给我回复下。菜鸟先谢过了。那没办法,我只能去服务器把php.ini配置修改了。经理说不让修改的,为了测试,偷偷改了--最后修改回来了。修改之后,测试,还是不行。还是到一分钟 就执行超时。真的很纳闷。不知道什么原因。求指教。那没办法。
这种办法行不通了,对一个5m的文件只能分行读取了。然后就是对代码的一通修改,分行读取是这样操作的,先ajax请求,然后每次读取2000条 然后对这2000条数据进行处理,插入数据库(文章最后介绍一个好用的分行读取函数)。然后每次ajax执行完,返回一个状态符,和本次读取到的行数,然后下次接着读。知道最后读取完。这中间还遇到一个问题:就是当我对每一行数据进行查重的时候遇到的,是这样的,我对得到的内容进行循环,然后查一下每行是否存在,当我判断$count是否大于0 的时候,当已存在的时候,我用continue,执行下一次循环。但是当我在导入10000条的时候,总是在8000条的时候报错说 服务器内部错误。很闷,不解问什么,结果只能用if else代替了。纳闷。一个小提醒:插入数据库的时候 不要一条一条的插入,最好这样 inset into aaa(`xx`,`xxx`)values('111','111'),('222','222')。这样 速度会快很多。
行号读取函数,SplFileObject这个类库真的很好用推荐。有知道我的问题的,求大神指教。
function getFileLines($filename, $startLine, $endLine, $method = 'rb'){
$content = array();
$filename = DATA_PATH.DS.'import' . DS . $filename;
$count = $endLine - $startLine;
$fp = new SplFileObject($filename, $method);
$fp->seek($startLine); // 转到第N行, seek方法参数从0开始计数
for ($ii = 0; $ii $content[] = $fp->current(); // current()获取当前行内容
$fp->next(); // 下一行
}
return array_filter($content); // array_filter过滤:false,null,''
}

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

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

Hot Article

Hot Tools

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

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

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

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.

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.

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.
