2011-01-12 49 views
1

我正在嘗試使用PHP發送.CSV文件。該文件在發送之前寫入磁盤,但是當我嘗試使用file_get_contents()附加文件時;當試圖發送它創建之前創建的文件時,.CSV的結構尚未預先保存。我得到一個資源ID(#183),那麼如何將用戶可打開的文件附加爲.CSV文件?我做了一定的MIME類型和頭是正確的CSV file_get_contents發送PHP

編輯

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, '[email protected]', CakeToppers, NULL, NULL, $attachment); 
    return true; 
     } 
+2

我不認爲這個問題是`的file_get_contents()`。你如何添加文件併發送消息? – jeroen 2011-01-12 13:30:56

回答

1

如果使用的是斯威夫特梅勒,沒有必要爲file_get_contents(),你可以attach the file directly

從斯威夫特梅勒文檔:

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