我需要生成併發送日誌數據到csv
格式的郵件。這裏是我的代碼:如何閱讀和發送資源?
public function sendMail(){
$csv = $this->createCSV();
$subject = $this->getMessage('subject');
$message = $this->getMessage('message');
$headers = $this->generateHeaders($csv, $message);
mail('[email protected]', $subject, $message, $headers);
}
private function createCSV(){
$data = $this->getAVG();
$csv = fopen('php://temp', 'w+');
// long statistics generation
fclose($csv);
return $csv;
}
private function generateHeaders($file, $message){
$boundary = md5(uniqid(time()));
$headers = 'MIME-Version: 1.0'."\r\n";
$headers .= 'Content-Type: multipart/mixed; boundary="'.$boundary."\"\r\n";
$headers .= 'From: [email protected]'.$_SERVER['SERVER_NAME']."\r\n";
$headers .= '--'.$boundary."\r\n";
$headers .= "Content-Type: text/html; charset=utf-8\r\n";
$headers .= "Content-Transfer-Encoding: base64\r\n";
$headers .= "\r\n";
$headers .= chunk_split(base64_encode($message));
$headers .= '--'.$boundary."\r\n";
$headers .= "Content-Type: text/csv; name=\"ServersLogs.csv\"\r\n";
$headers .= "Content-Transfer-Encoding: base64\r\n";
$headers .= "Content-Disposition: attachment; filename=\"ServersLogs.csv\"\r\n";
$headers .= "\r\n";
$headers .= chunk_split(base64_encode(readfile($file)));
$headers .= "\r\n--".$boundary."--\r\n";
return $headers;
}
的問題是,我不能保存文件的服務器上,所以我用php://,之後卻無法讀取文件...
你有沒有試過將文件保存在'/ tmp'上?你應該有權限在那裏。 – fedorqui 2013-02-20 09:49:26