【问题标题】:CSV file_get_contents sending PHPCSV file_get_contents 发送 PHP
【发布时间】:2011-01-12 13:28:43
【问题描述】:

我正在尝试使用 PHP 发送一个 .CSV 文件。该文件在发送之前已写入磁盘,但是当我尝试使用 file_get_contents() 附加文件时;当尝试发送在发送之前创建的文件时,.CSV 的结构还没有被保留,我得到一个资源 ID (#183) 那么我如何附加一个用户可以作为 .CSV 文件打开的文件?我已确保 mime 类型和标题正确

编辑

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;
        }

【问题讨论】:

  • 我认为问题不在于file_get_contents()。您如何添加文件并发送消息?
  • 我正在使用 Swift Mailer 发送电子邮件。我记得我不久前使用 file_get_contents() 或 fopen() 或类似的东西得到了这个工作,但我已经尝试了将它们与字符串和资源一起使用的所有组合,但没有运气。我知道 CSV 文件已成功创建,因为创建后我可以查看它们,所以我不知所措。
  • 请贴一些代码。看起来您正在使用一个作为处理程序的变量

标签: php csv file-get-contents


【解决方案1】:

如果您使用的是 Swift Mailer,则不需要 file_get_contents(),您只需 attach the file directly

来自 Swift Mailer 文档:

//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);

所以对你来说是:

$attachment = Swift_Attachment::fromPath(_PS_ORDERS_DIR_.$orderDate.'/'.$file_name.'.csv', 'application/vnd.ms-excel');  

//Attach it to the message
$message->attach($attachment);

【讨论】:

    猜你喜欢
    • 2021-12-15
    • 2011-10-17
    • 1970-01-01
    • 2021-10-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-21
    相关资源
    最近更新 更多