Search code examples
phpcsvcodeigniter-2

CSV generated file in PHP : last value is truncated


In CodeIgniter 2, I'm generating a CSV file from fetching data from my Oracle Database (volume of data can be a less than 10 lines to hundreds of thousands of lines, this is why I "unset" each $line) using the following code :

        $conn = $this->db;

        $stid = oci_parse($conn->conn_id, $sql);
        oci_execute($stid);

        // Header extraction
        $headers = "";
        $values = "";

        $row = oci_fetch_array($stid, OCI_ASSOC + OCI_RETURN_NULLS );

        foreach($row as $key => $value) {
            $headers = $headers.$key.';';
            $values = $values.$value.';';
        }
        // Remove of last comma and adding in a line break
        $headers = substr($headers,0,-1). "\r\n";
        $values = substr($values,0,-1). "\r\n";
        $data = $headers . $values;

        $values = "";

        // Parsing all data to concatenate the values
        while (($row = oci_fetch_array($stid, OCI_ASSOC + OCI_RETURN_NULLS)) != false) {
            $line = "";
            foreach ($row as $key => $value) {
                $line .= $value . ';';
            }
            $line = substr($line, 0, -1) . "\n";
            $data = $data . $line;
            // Freeing memory for the line
            unset($line);
        }
        return $data;

Using this piece code, my CSV file is properly generated except for one thing.

When I'm exporting just a small number of lines, everything is fine. I have all the values, for ALL lines, comma seperated. However, when I'm exporting a few hundred or thousand of lines, the LAST value from the LAST line is always truncated by 5 characters (data in the database is OK).

HEADER1;HEADER2;HEADER3;HEADER4;HEADER5
1-XXXXXX;F1;IDX1;ERR_IDX_CAX_0001
1-XXXXXX;F1;IDX1;ERR_IDX_CAX_0001
1-XXXXXX;F1;IDX1;ERR_IDX_CAX_0001
1-XXXXXX;F1;IDX1;ERR_IDX_CAX_0001
1-XXXXXX;F1;IDX1;ERR_IDX_CAX_0001
[.......]
1-XXXXXX;F1;IDX1;ERR_IDX_CAX_0001
1-XXXXXX;F1;IDX1;ERR_IDX_CAX

I thought maybe it was some cache limitation or something, but it happens after a few hundred lines exported and wether it's just a few hundred or several hundred thousand lines.

I can't figure this one out ...

Can anybody help ?

Thanks in advance.


Solution

  • Original issue described in this post not related to the code.

    The issue comes from the Header Content Length passed by CodeIgniter force_download() function (from CI download helper).

    header("Content-Length: ".strlen($data));
    

    strlen($data) doesn't return (in my case) the correct length of data and my browser then only downloads the amount of data that was passed in the header, hence truncating a few characters at the end.

    Not finding anything on this "issue", I decided to comment out the header Content length line