Generate CSV File and Force Browser to Download It in CodeIgniter

So, as the title implies, this is not general PHP approach, it is about how to do that in CodeIgniter.


Here is the solution found from the Internet: Excel CSV helper for CodeIgniter


But this snippet won't work for UTF-8 encoded characters. So I copied the code in the comment (posted by email@williamli.hk) discussing that issue:

if ($download != "")
{
	/*
	header('Content-Encoding: UTF-8');
	header('Content-Type: text/csv; charset=UTF-8');
	//header('Content-Type: application/csv; charset=UTF-8');
	header('Content-Disposition: attachment; filename="' . $download . '"');
	echo "\xEF\xBB\xBF"; // UTF-8 BOM
	*/

	header('Content-Description: File Transfer');
	//header('Content-Type: application/octet-stream');
	header("Content-Type: application/vnd.ms-excel; charset=UTF-16LE");
	header('Content-Disposition: attachment; filename='.$download);
	header('Content-Transfer-Encoding: binary');
	header('Expires: 0');
	header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
	header('Pragma: public');
	//echo "\xEF\xBB\xBF"; // UTF-8 BOM
	echo "\xFF\xFE"; // UTF-16 LE
}


My final code came to be:

if ( ! function_exists('array_to_csv'))
{
	function array_to_csv($array, $download = "")
	{
		$CI =& get_instance();
		
		if ($download != "")
		{	
			header('Content-Encoding: UTF-8');
			header('Content-Type: text/csv; charset=UTF-8');
			header('Content-Disposition: attachment; filename="' . $download . '"');
			echo "\xEF\xBB\xBF"; // UTF-8 BOM
		}		

		ob_start();
		$f = @fopen('php://output', 'w');
		if (FALSE === $f)
		{
			$this->CI->app88log->log_message('csv', 'error', "Can't open php://output");
			show_error($this->CI->lang->line('admin_export_excel_error_message'));
		}
		
		$n = 0;		
		foreach ($array as $line)
		{
			$n++;
			try {
				$res = fputcsv($f, $line);
				if (FALSE === $res) 
				{
					$this->CI->app88log->log_message('csv', 'error', 'Can not write line' . $n );
					show_error($this->CI->lang->line('admin_export_excel_error_message'));
				}
			}
			catch (Exception $e) {
				$this->CI->app88log->log_message('csv', 'error', $e->getMessage());
				show_error($this->CI->lang->line('admin_export_excel_error_message'));
			}
		}
		
		fclose($f);
		$str = ob_get_contents();
		ob_end_clean();

		if ($download == "")
		{
			return $str;	
		}
		else
		{
			echo $str;
		}		
	}
}

And it was saved under /application/helper/.


Then load it in controllers' constructor, and call it in method:

		$fileName = 'product-' . gmdate('Y-m-d') . '.csv';
		array_to_csv($csvData, $fileName);
		exit;

Pass the two-dimension array of data to the function and let it take care of all the job left.





I'm sorry, as an AI language model, I don't have access to your local environment and cannot execute commands on your behalf. However, I can provide you with general instructions on how to run the protobuf compiler. To generate a file using the protobuf compiler, you need to follow these steps: 1. Install the protobuf compiler: You can download and install the protobuf compiler from the official website (https://developers.google.com/protocol-buffers/docs/downloads). 2. Define your message schema: You need to create a .proto file that defines the structure of your message. 3. Compile your message schema: Use the protobuf compiler to compile your .proto file. The command to compile a .proto file is: ``` protoc --proto_path=IMPORT_PATH --python_out=OUTPUT_DIR INPUT_FILE ``` Where: - `IMPORT_PATH`: The directory containing any .proto files that your schema imports. - `OUTPUT_DIR`: The directory where you want the generated Python files to be saved. - `INPUT_FILE`: The path to your .proto file. For example, if you have a file named `my_message.proto` in the current directory and you want to generate the Python code in a folder named `generated`, you can run: ``` protoc --proto_path=. --python_out=generated my_message.proto ``` 4. Use the generated code: Once you have generated the Python code, you can import it in your Python project and use it to serialize and deserialize your messages. The generated code will contain a Python class for each message type defined in your .proto file. I hope this helps! Let me know if you have any further questions.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值