Home Backend Development PHP Tutorial php+ajax导入大数据时产生的问题处理_php实例

php+ajax导入大数据时产生的问题处理_php实例

Jun 07, 2016 pm 05:19 PM
ajax php Big Data

遇到的问题就从先到后的一一说吧。

问题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')。这样 速度会快很多。

php+ajax导入大数据时产生的问题处理_php实例

行号读取函数,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,''
}
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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

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)

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

To work on file upload we are going to use the form helper. Here, is an example for file upload.

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

In this chapter, we are going to learn the following topics related to routing ?

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

CakePHP Creating Validators CakePHP Creating Validators Sep 10, 2024 pm 05:26 PM

Validator can be created by adding the following two lines in the controller.

See all articles