How to cause a .csv file to be downloaded using PHP after sending the .csv via email? [closed]
$mailflg = $SiteObj->Sendsmtpmail($FromMail,$Email,"Check",$trackerCode,$smtpDetails);
if(!$mailflg)
{
$myFile = "../csv/".$my_file_name;
$smtpDetails['Test'] = "FAIL";
$file = @fopen($myFile, "a");
if($file)
{
fwrite($file,$smtpDetails['HOST']. ",".$smtpDetails['USER'].",".$smtpDetails['PASSWORD'].",".$smtpDetails['PORT'].",".$smtpDetails['NOEMAIL'].",".$smtpDetails['SSL'].",".$smtpDetails['FROM'].",".$smtpDetails['Test']. "\r\n");
fclose($file);
}
}
else
{
$myFile = "../csv/".$my_file_name;
$smtpDetails['Test'] = "SEND";
$file = @fopen($myFile, "a");
if($file)
{
fwrite($file,$smtpDetails开发者_开发技巧['HOST']. ",".$smtpDetails['USER'].",".$smtpDetails['PASSWORD'].",".$smtpDetails['PORT'].",".$smtpDetails['NOEMAIL'].",".$smtpDetails['SSL'].",".$smtpDetails['FROM'].",".$smtpDetails['Test']. "\r\n");
fclose($file);
}
}
After the email is sent, how do I cause the .csv file to be downloaded to the client machine? (open file dialog)
To send the CSV file down to the browser, you need to supply the appropriate headers and then just send its data to the output buffer:
// Your other file handling code above here....
if($file)
{
fwrite($file,$smtpDetails['HOST']. ",".$smtpDetails['USER'].",".$smtpDetails['PASSWORD'].",".$smtpDetails['PORT'].",".$smtpDetails['NOEMAIL'].",".$smtpDetails['SSL'].",".$smtpDetails['FROM'].",".$smtpDetails['Test']. "\r\n");
fclose($file);
// The file was written and closed, now send it to the browser...
header("Content-type: text/csv");
header("Content-description: File Transfer");
header("Content-disposition: attachment; filename=\"thefilename.csv\"");
header("Pragma: public");
header("Cache-control: max-age=0");
header("Expires: 0");
// readfile() will progressively read from disk and send data to the output buffer
readfile($myFile);
}
Normally, a CSV is downloaded this way:
/* Set your headers: */
//Content type.
header("Content-type: application/csv");
//replace file.csv with whatever you want to have as a file.
header("Content-Disposition: attachment; filename=file.csv");
// Prevent caching.
header("Pragma: no-cache");
header("Expires: 0");
readfile('/path/to/file');
i want to write the host,user,password in my csv file, right. after that i want to download that csv file i got the download prompt But when i get that downloaded file i got that data with my design page code... –
I'm trying to guess what you mean, so I've offered 3 possibilities I can think of for your situation.
1) If by this you mean the CSV writing works when you run it from the command line, but when you use the browser, it shows you PHP code, make sure your web server is setup to run PHP code correctly. Make a test program of something like test.php:
<?php echo 'Test Output'; ?>
And make sure all you see is 'Test Output' in your browser, and not php code. PHP 3+ requires the open tag to have the word 'php' by default, contrary to prior versions where you could just use the question mark.
The answers given already are right and should not result in PHP code in the downloaded file.
2) If by 'design page code', you meant you see HTML and other NON-PHP, NON-CSV output in your browser, make sure you don't output any further data to the browser after supplying the CSV headers given in the previous answers. For instance, put an 'exit;' immediately after the readfile or get_file_contents.
This will prevent any extra HTML or etc to be shown in your downloaded CSV file. In the part of your code the writes the CSV file for download, you can't control the browser behavior after the download. You need to do that in the HTML before providing the download box, like if you put a page that shows a link to download the CSV, then you could control the browser with javascript on the link, or just let it stay on the current page.
3) If by 'design page code', you meant that the file isn't really downloading and you're seeing the CSV on the web page in the browser, make sure your script isn't outputting anything to the browser before the 'header()' function calls preceding the CSV output. For instance, make sure there are no PHP print() or echo() calls, and that there's no blank linkes in your PHP files before the opening php tags, or after the closing tags in any included files. Typically in PHP files that are only meant as include files, you shouldn't even put closing tags, for this reason.
http://www.sitepoint.com/should-you-close-your-php-code-tags/
精彩评论