search
HomeBackend DevelopmentPHP Tutorialajax大数据导入的一系列相关问题处理_PHP教程

介绍:就是想实现简单的ajax上传数据,但是当数据量较大的时候,问题就一个一个接着来了,其实数据也不是很大,就是csv格式数据 不到5w条数据。大小5M,一开始认为这个很简单,就是先上传一下文件,然后读取一下,存到数据库就好了,结果,可能我比较菜,弄了半天做出这个功能。环境是linux.

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

问题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,''
}

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/781156.htmlTechArticle介绍:就是想实现简单的ajax上传数据,但是当数据量较大的时候,问题就一个一个接着来了,其实数据也不是很大,就是csv格式数据 不到...
Statement
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
11 Best PHP URL Shortener Scripts (Free and Premium)11 Best PHP URL Shortener Scripts (Free and Premium)Mar 03, 2025 am 10:49 AM

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

Introduction to the Instagram APIIntroduction to the Instagram APIMar 02, 2025 am 09:32 AM

Following its high-profile acquisition by Facebook in 2012, Instagram adopted two sets of APIs for third-party use. These are the Instagram Graph API and the Instagram Basic Display API.As a developer building an app that requires information from a

Working with Flash Session Data in LaravelWorking with Flash Session Data in LaravelMar 12, 2025 pm 05:08 PM

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-

Build a React App With a Laravel Back End: Part 2, ReactBuild a React App With a Laravel Back End: Part 2, ReactMar 04, 2025 am 09:33 AM

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

Simplified HTTP Response Mocking in Laravel TestsSimplified HTTP Response Mocking in Laravel TestsMar 12, 2025 pm 05:09 PM

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' =>

cURL in PHP: How to Use the PHP cURL Extension in REST APIscURL in PHP: How to Use the PHP cURL Extension in REST APIsMar 14, 2025 am 11:42 AM

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.

12 Best PHP Chat Scripts on CodeCanyon12 Best PHP Chat Scripts on CodeCanyonMar 13, 2025 pm 12:08 PM

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

Announcement of 2025 PHP Situation SurveyAnnouncement of 2025 PHP Situation SurveyMar 03, 2025 pm 04:20 PM

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

See all articles

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 agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),