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);
}
}