forked from chetans9/core-php-admin-panel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexport_customers.php
44 lines (30 loc) · 1.12 KB
/
export_customers.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
<?php
session_start();
require_once 'config/config.php';
require_once BASE_PATH . '/includes/auth_validate.php';
$db = getDbInstance();
$select = array('id', 'f_name', 'l_name', 'gender', 'phone', 'created_at', 'updated_at');
$chunk_size = 100;
$offset = 0;
$data = $db->withTotalCount()->get('customers');
$total_count = $db->totalCount;
$handle = fopen('php://memory', 'w');
fputcsv($handle,$select);
$filename = 'export_customers.csv';
$num_queries = ($total_count/$chunk_size) + 1;
//Prevent memory leak for large number of rows by using limit and offset :
for ($i=0; $i<$num_queries; $i++){
$rows = $db->get('customers',Array($offset,$chunk_size), $select);
$offset = $offset + $chunk_size;
foreach ($rows as $row) {
fputcsv($handle,array_values($row));
}
}
// reset the file pointer to the start of the file
fseek($handle, 0);
// tell the browser it's going to be a csv file
header('Content-Type: application/csv');
// Save instead of displaying csv string
header('Content-Disposition: attachment; filename="'.$filename.'";');
//Send the generated csv lines directly to browser
fpassthru($handle);