Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7574105
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T16:21:10+00:00 2026-05-30T16:21:10+00:00

I am using the current code to read a csv file and add it

  • 0

I am using the current code to read a csv file and add it to an array:

    echo "starting CSV import<br>";
    $current_row = 1; 
    $handle = fopen($csv, "r"); 
    while ( ($data = fgetcsv($handle, 10000, ",") ) !== FALSE ) 
    { 
        $number_of_fields = count($data); 
        if ($current_row == 1) { 
        //Header line 
            for ($c=0; $c < $number_of_fields; $c++) 
            { 
                $header_array[$c] = $data[$c]; 
            } 
        } else { 
        //Data line 
            for ($c=0; $c < $number_of_fields; $c++) 
            { 
                $data_array[$header_array[$c]] = $data[$c]; 
            } 

            array_push($products, $data_array);

        } 
        $current_row++; 
    } 
    fclose($handle); 
    echo "finished CSV import <br>";

However when using a very large CSV this times out on the server, or has a memory limit error.

I’d like a way to do it in stages, so after the first say 100 lines it will refresh the page, starting at line 101.

I will probably be doing this with a meta refresh and a URL parameter.

I just need to know how to adapt that code above to start at the line I tell it to.

I have looked into fseek() but I’m not sure how to implement this here.

Can you please help?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T16:21:12+00:00Added an answer on May 30, 2026 at 4:21 pm

    The timout can be circumvented using

    ignore_user_abort(true);
    set_time_limit(0);
    

    When experiencing problems with the memory limit, it may be wise to take a step back and look at what you’re actually doing with the data you’re processing. Are you pushing the data into a database? calculate something off the data but don’t need to store the actual data, …

    Do you really need to push (array_push($products, $data_array);) the rows into an array (for later processing)? can you instead write to the database directly? or calculate directly? or build an html <table> directly? or whatever the hell you’re doing right then an there, within the while() loop, without pushing everything into an array first?

    If you’re able to chunk the processing, I guess you don’t need that array at all. Otherwise you’d have to restore the array for every chunk – not solving the memory issue one bit.

    If you can manage to change your processing algorithm to waste less memory / time, you should seriously consider that over any chunked processing requiring a round-trip to the browser (for so many performance and security reasons…).

    Anyways, you can, at any time, identify the current stream offset with ftell() and re-set to that position using fseek(). You’d only need to pass that integer to your next iteration.


    Also there is no need for your inner for() loops. This should produce the same results:

    <?php
    
    $products = array();
    $cols = null;
    $first = true; 
    
    $handle = fopen($csv, "r"); 
    while (($data = fgetcsv($handle, 10000, ",")) !== false)  { 
        if ($first) { 
            $cols = $data;
            $first = false;
        } else { 
            $products[] = array_combine($cols, $data);
        }
    }
    
    fclose($handle); 
    echo "finished CSV import <br>";
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How can I read a Chinese text file using C#, my current code can't
I am trying to load a CSV file into an array using ColdFusion (version
Here is the current code I am using: <UserControl xmlns=http://schemas.microsoft.com/winfx/2006/xaml/presentation xmlns:x=http://schemas.microsoft.com/winfx/2006/xaml x:Class=ButtonPrototype.MainPage Width=640 Height=480>
I find myself using the current pattern quite often in my code nowadays var
Why does this code fail to display the category name Apples using the current
On my current project, I am using FxCop to work through various code analysis
My current setup using hibernate uses the hibernate.reveng.xml file to generate the various hbm.xml
My goal here is to have the browser download a csv file using headers
This is my current code: public void WriteXml(System.Xml.XmlWriter writer) { CloseConnection(); using (Stream source
I have a strange issue: I am using SPContext.Current.Web in a .aspx page, but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.