douliao1911 2011-01-12 13:28
浏览 73

CSV file_get_contents发送PHP

I'm trying to send a .CSV file with PHP. The file is written to disk before it's sent but when I try to attach the file with file_get_contents(); the structure of the .CSV isn't preseved yet when try and send the file that's created before it's sent I get a resource id (#183) so how can i attach a file which the user can open as a .CSV file? I've made sure the mime type and headers are correct

EDIT

if(!file_exists(_PS_ORDERS_DIR_.$orderDate.'/'.$file_name.'.csv'))
{
        if($file = fopen (_PS_ORDERS_DIR_.$orderDate.'/'.$file_name.'.csv', 'x+'))
        {
        foreach ($list as $fields)
            {
                fputcsv($file, $fields);
            }


            $attachment['mime'] = 'application/vnd.ms-excel';
            $attachment['content'] = file_get_contents(_PS_ORDERS_DIR_.$orderDate.'/'.$file_name.'.csv');
            $attachment['name'] = $order.'order';

    Mail::Send(1, 'order_conf', 'Order CSV Attachment', $success, 'dan.farr@gmail.com', CakeToppers, NULL, NULL, $attachment);
    return true;
        }
  • 写回答

1条回答 默认 最新

  • donglan8999 2011-01-12 13:36
    关注

    If you are using Swift Mailer, there is no need for file_get_contents(), you can just attach the file directly.

    From the Swift Mailer documentation:

    //Create the attachment
    // * Note that you can technically leave the content-type parameter out
    $attachment = Swift_Attachment::fromPath('/path/to/image.jpg', 'image/jpeg');  
    
    //Attach it to the message
    $message->attach($attachment);
    

    So for you that would be:

    $attachment = Swift_Attachment::fromPath(_PS_ORDERS_DIR_.$orderDate.'/'.$file_name.'.csv', 'application/vnd.ms-excel');  
    
    //Attach it to the message
    $message->attach($attachment);
    
    评论

报告相同问题?

悬赏问题

  • ¥15 在获取boss直聘的聊天的时候只能获取到前40条聊天数据
  • ¥20 关于URL获取的参数,无法执行二选一查询
  • ¥15 液位控制,当液位超过高限时常开触点59闭合,直到液位低于低限时,断开
  • ¥15 marlin编译错误,如何解决?
  • ¥15 有偿四位数,节约算法和扫描算法
  • ¥15 VUE项目怎么运行,系统打不开
  • ¥50 pointpillars等目标检测算法怎么融合注意力机制
  • ¥20 Vs code Mac系统 PHP Debug调试环境配置
  • ¥60 大一项目课,微信小程序
  • ¥15 求视频摘要youtube和ovp数据集