2

Here's the problem I wanted to convert my data into csv format and download it. everything is fine, until the csv file that i had downloaded and there's a little bug on the file which there will be a space at the first line of the file.

for example Before force download

"name","age",
"brad pit","40",`

After force download


"name","age",
"brad pit","40",

The csv file that i had downloaded and I try to open wit my excel will appear like this
"name" |age
brad pit|40

I believe that is because of the csv file that i had downloaded appeared an external space line in the first line of the data.

Here's the code

//write csv data
$data = $this->dbutil->csv_from_result($query, $delimiter);
//create random file name
$name = rand().'_salesperson_data_'.date('d-m-y').'.csv';

if ( ! write_file('./csv/'.$name, $data))
{
     echo 'Unable to write the CSV file';
}
else
{
    //perform download
    $file = file_get_contents("./csv/".$name); // Read the file's contents
    $filename = 'salesperson_data_'.date('d-m-y').'.csv';
    force_download($filename, $file);
}

source of force_download()

if ( ! function_exists('force_download'))
{
    function force_download($filename = '', $data = '')
    {
               
        if ($filename == '' OR $data == '')
        {
            return FALSE;
        }

        // Try to determine if the filename includes a file extension.
        // We need it in order to set the MIME type
        if (FALSE === strpos($filename, '.'))
        {
            return FALSE;
        }
    
        // Grab the file extension
        $x = explode('.', $filename);
        $extension = end($x);

        // Load the mime types
        @include(APPPATH.'config/mimes'.EXT);
    
        // Set a default mime if we can't find it
        if ( ! isset($mimes[$extension]))
        {
            $mime = 'application/octet-stream';
        }
        else
        {
            $mime = (is_array($mimes[$extension])) ? $mimes[$extension][0] : $mimes[$extension];
        }
    
        // Generate the server headers
        if (strstr($_SERVER['HTTP_USER_AGENT'], "MSIE"))
        {
            header('Content-Type: "'.$mime.'"');
            header('Content-Disposition: attachment; filename="'.$filename.'"');
            header('Expires: 0');
            header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
            header("Content-Transfer-Encoding: binary");
            header('Pragma: public');
            header("Content-Length: ".strlen($data));
        }
        else
        {
            header('Content-Type: "'.$mime.'"');
            header('Content-Disposition: attachment; filename="'.$filename.'"');
            header("Content-Transfer-Encoding: binary");
            header('Expires: 0');
            header('Pragma: no-cache');
            header("Content-Length: ".strlen($data));
        }
    
        exit($data);
    }
}

i thought TRIM will be last solution for me and I try to put any where possible but is stil the same. I couldn't found any solution for this problem. Please help. this stuck me for 2days already.

3
  • show us the source of the force_download() method please? Commented May 18, 2011 at 14:03
  • after running the query do: print_r($query); exit; and tell us what you get? Commented May 18, 2011 at 18:53
  • Can you add the code for CSV_from_result() please. Commented May 31, 2011 at 22:22

3 Answers 3

1

I don't know if need to use CSV only, but a good plugin/function that I use is this one: http://codeigniter.com/wiki/Excel_Plugin/

It's works with CodeIgniter Query system for exporting stuff to Excel. I use it a lot and never have problems with it.

Sign up to request clarification or add additional context in comments.

1 Comment

i found another plugin which export to csv, but when using IE, it doesn't appear to download but just open the csv with my excel straight away. i will try your plugin, thx anyway.
1

try to print on the browser, if you see some extra space then remove.

if the extra is still on the csv file when you download, then this extra space is coming from any of your include file.

when you start writing your code try not to leave some space on the top/bottom of the code.

Comments

0

Use ob_clean(); before writing CSV to remove White spaces.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.