0

I'm trying to export a list of contacts with a specified set of fields from in Codeigniter. PHP is exiting early and I can't figure out why. It exports 4,092 contacts then exits but the count of the array being exported is 140,699. PHP gives me no errors, and on my test server the export function works fine. Here's the code:

function admin_export()
{
    set_time_limit(0);

    if(!$this->ion_auth->in_group(array('admin')) && !$this->input->is_cli_request())
        die();


    $contacts = $this->contacts_model->get_contacts();
    $export_fields = unserialize($this->contacts_model->get_contact_export_fields());

    if(!file_exists($this->config->item('tmp_path')))
            mkdir($this->config->item('tmp_path'));

    if($export_fields == false)
    {
        echo json_encode(false);
    }
    else 
    {
        $fh = fopen($this->config->item('tmp_path').'export.csv', 'w');

        fputcsv($fh, $export_fields, ',');

        foreach($contacts as $i => $contact)
        {
            $id = $contact['id'];

            foreach($contact as $k => $v)
            {
                if(!in_array($k, $export_fields))
                {
                    unset($contacts[$i][$k]);
                }
            }

            if(in_array('role', $export_fields))
            {
                $contacts[$i]['role'] = '';

                $roles = $this->contacts_model->get_contact_roles($id);
                foreach($roles as $role)
                {
                    $contacts[$i]['role'] .= $role['role'].';';
                }

                $contacts[$i]['role'] = rtrim($contacts[$i]['role'], ';');
            }

            if(in_array('role_id', $export_fields))
            {
                $contacts[$i]['role_id'] = '';

                $role_ids = $this->contacts_model->get_contact_roles($id);
                foreach($role_ids as $role_id)
                {
                    $contacts[$i]['role_id'] .= $role_id['id'].';';
                }

                $contacts[$i]['role_id'] = rtrim($contacts[$i]['role_id'], ';');
            }

            fputcsv($fh, $contacts[$i], ',');
        }
        fclose($fh);

        echo json_encode(true);
    }
}
adam
  • 463
  • 1
  • 7
  • 15
  • 1
    what about the execution time? – ITroubs Jan 16 '15 at 18:46
  • I thought set_time_limit(0); gave php infinite execution time – adam Jan 16 '15 at 18:47
  • see here: http://stackoverflow.com/questions/11999678/php-timeout-set-time-limit0-dont-work – ITroubs Jan 16 '15 at 18:52
  • Just to quote the php documentation of that function: This function has no effect when PHP is running in safe mode. There is no workaround other than turning off safe mode or changing the time limit in the php.ini. – ITroubs Jan 16 '15 at 18:56
  • Set the error_reporting to `E_ALL` – Riad Jan 16 '15 at 19:04
  • I figured it out after setting the environment to development which sets error reporting to `E_ALL`. The maximum amount of memory was being exhausted – adam Jan 16 '15 at 19:24

1 Answers1

1

What about memory? It's possible that it is maxing out the allocated memory and stopping.

mcdonaldjosh
  • 119
  • 2
  • 8