mysql - my CSV attachment is empty! PHP -
this question exact duplicate of:
- php attachment in email empty 1 answer
im trying take data database , display in csv file both downloadable , emailed someone. have managed downloadable file working , displays of correct data. sends csv file necessary person csv file empty , no data displayed in it.
here code:
$myroot = "../../"; include($myroot . "inc/functions.php"); // output headers file downloaded rather displayed header('content-type: text/csv; charset=utf-8'); header('content-disposition: attachment; filename=surveys.csv'); $output = fopen('php://output', 'w'); // create csv file fputcsv($output, array('name', 'branch', 'website','company', 'question1', 'question2', 'question3', 'question4', 'question5')); $mysql_connection = db_connect_enhanced('*****','*****','*****','*****'); $query='select * *****.*****'; $surveys = db_query_into_array_enhanced($mysql_connection, $query); $count = count($surveys); $data = array(); for($i=0; $i<=$count; $i++){ $data[] = array($surveys[$i]['feedbackname'], $surveys[$i]['branchname'], $surveys[$i]['feedbackwebsite'], $surveys[$i]['feedbackcompany'], $surveys[$i]['question1'], $surveys[$i]['question2'], $surveys[$i]['question3'], $surveys[$i]['question4'], $surveys[$i]['question5']); } foreach( $data $row ) { fputcsv($output, $row, ',', '"'); } fclose($output); $encoded = chunk_split(base64_encode($output)); // create email , send off $subject = "file requested rrwh.com"; $from = "*****@*****.com"; $headers = 'mime-version: 1.0' . "\n"; $headers .= 'content-type: multipart/mixed; boundary="----=_nextpart_001_0011_1234abcd.4321fdac"' . "\n"; $message = ' multi-part message in mime format. ------=_nextpart_001_0011_1234abcd.4321fdac content-type: text/plain; charset="us-ascii" content-transfer-encoding: 7bit hello have attached php script requested http://rrwh.com/scripts.php zip file. regards ------=_nextpart_001_0011_1234abcd.4321fdac content-type: application/octet-stream; name="'; $message .= "surveys.csv"; $message .= '" content-transfer-encoding: base64 content-disposition: attachment; filename="'; $message .= "surveys.csv"; $message .= '" '; $message .= $encoded; $message .= ' ------=_nextpart_001_0011_1234abcd.4321fdac-- '; mail("*****@*****.com", $subject, $message, $headers, "-f$from");
i've spent day , half on cant see problem. please point out me why attached csv file empty?
i'm getting kind of desperate , stressed out :( please me.
base64_encode()
expects parameter string , give (closed) resource. try read resource string, or use file_get_contents
or build string while write resource.
update:
try , replace
foreach( $data $row ) { fputcsv($output, $row, ',', '"'); } fclose($output); $encoded = chunk_split(base64_encode($output));
by
$myoutput = '"name","branch","website","company","question1","question2","question3","question4","question5"'; foreach( $data $row ) { $myoutput .= "\"".implode('","',$row)."\"\n"; fputcsv($output, $row, ',', '"'); } fclose($output); $encoded = chunk_split(base64_encode($myoutput));
this way, write output write new variable ($myoutput
). since string can use base64_encode()
.
Comments
Post a Comment