【问题标题】:How to save mysql result as csv in specific folder in codeigniter如何在codeigniter的特定文件夹中将mysql结果保存为csv
【发布时间】:2019-08-03 10:07:29
【问题描述】:

我想发送一封带有来自 MySQL 结果的 csv 附件的电子邮件。 当我调用 URL 时,我的代码直接下载了文件。但是邮件中没有附件。我想在电子邮件中作为附件发送。

public function retrieveEmail($s){
        //print_r($s);
        $config = Array(
                     'protocol' => 'smtp',
                     'smtp_host' => getenv('SMTP_HOST_'.getenv('ENV_TYPE')),
                    'smtp_port' => getenv('SMTP_PORT_'.getenv('ENV_TYPE')),
                    'smtp_user' => getenv('SMTP_USER_'.getenv('ENV_TYPE')),
                    'smtp_pass' => getenv('SMTP_PASS_'.getenv('ENV_TYPE')),
                    'smtp_timeout' => 5,
                     'mailtype' => 'html',
                     'charset' => 'iso-8859-1',
                     'wordwrap'  => TRUE,
                     'newline'   => "\r\n"
                );
        $this->load->library('email', $config);
        //echo "Hi";
        $this->email->initialize($config);
        $this->email->set_newline("\r\n");
        $subject = "Serials blocked report";
        $econtent = "Testing purpose only";
        $filename = "serialblockingreport_".date('Y-m-d').".csv";
        header("Content-type: text/csv");
        header("Content-Disposition: attachment; filename=" . $filename);
        header("Pragma: no-cache");
        header("Expires: 0");
        $file = fopen("php://output","r");
        $header = array("id","serial","itemname","expire_date","isactive","blockedat"); 
        fputcsv($file, $header);
        foreach ($s as $key=>$line){ 
             fputcsv($file,$line); 
        }
        // force_download($file, $header);
        //fclose($file); 
        //exit; 
        //print_r($s);
        //print_r($r);
        $this->email->from(getenv('SMTP_FROM_'.getenv('ENV_TYPE')), 'Serial Block Statement');
        $this->email->to('pacepdtpro@gmail.com');
        $this->email->bcc('pacepdtpro1@gmail.com');       
        $this->email->subject($subject);
        $this->email->message($econtent);
        $this->email->attach($file);       
            if($this->email->send()) {

            } else {
                show_error($this->email->print_debugger());
            }
    }

【问题讨论】:

  • 使用csv库函数加载文件
  • stackoverflow.com/questions/57217875/… 请参阅此链接以供参考
  • 嗨,杰克我正在使用所有装载机。代码正在运行。但根据我的研究,我需要将生成的 csv 存储/保存在某个文件夹中。我已经将生成的 csv 文件存储在系统中

标签: php codeigniter


【解决方案1】:

我建议将您的文件保存在临时文件夹中,如果邮件发送成功,请将其发送并销毁: 示例:

$csv_content = array_to_csv($output);
$file = '/tmp/'.time().".csv";
if ( ! write_file($filename, $csv))
{
  //log the details of failed
} else {
  //sent the email with attachement
} 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-02-20
    • 1970-01-01
    • 1970-01-01
    • 2011-08-29
    • 1970-01-01
    • 1970-01-01
    • 2015-01-07
    • 2022-01-19
    相关资源
    最近更新 更多