簡體   English   中英

為什么我的CSV文件為空

[英]Why is my CSV file empty

因此,我有這個php腳本,在用戶將數據輸入表單后,該數據便被收集起來,並將其中一些放入.csv文件中,然后作為附件發送給用戶,但是我可以將電子郵件與附件一起發送但是.csv文件始終為空,我不知道為什么這是我的代碼

function send_csv_mail($csvData, $body, $to, $subject, $from)
{
    $multipartSep = '-----' . md5(time()) . '-----';

    $headers = [
        "From: $from",
        "Reply-To: $from",
        "Content-Type: multipart/mixed; boundary=\"$multipartSep\"",
    ];

    $fp = fopen('php://temp', 'w');

    fputcsv($fp, ["Supplier Number", "Supplier Name", "Product Code", "Product Description", "Unit of Measure", "Product Group", "Buy Price EX GST"]);

    $testData = [
        ['123', '456', '789', '012', '345', '678', '901'],
        ['abc', 'def', 'ghi', '123', '456', '789', '012'],
        ['jkl', 'mno', 'pqr', '123', '456', '789', '012'],
    ];

    foreach ($testData as $data) {

        fputcsv($fp, $data);
    }

    fclose($fp);

    $attachment = chunk_split(base64_encode(file_get_contents($fp)));

    $newBody = "--$multipartSep\r\n"
        . "Content-Type: text/html; charset=ISO-8859" . "\r\n"
        . "\r\n"
        . "$body" . "\r\n"
        . "--$multipartSep" . "\r\n"
        . "Content-Type: text/csv" . "\r\n"
        . "Content-Transfer-Encoding: base64" . "\r\n"
        . "Content-Disposition: attachment;             filename=\"New_Parts_and_Barcodes_Request.csv\"" . "\r\n"
        . "\r\n"
        . "$attachment" . "\r\n"
        . "--$multipartSep--";

    return @mail($to, $subject, $newBody, implode("\r\n", $headers));
}

對不起,我格式化第一篇文章:)

file_get_contents需要文件路徑作為參數,而不是文件指針。 嘗試將其更改為

$attachment = chunk_split(base64_encode(file_get_contents("php://temp")));

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM