Export to csv is working. But date format is ######## for date 11/11/2014. The result is
11 oct,14 for 11/7/2014. Error is during 2 digits for date. Could somebody solve the issue?
$fp = fopen('reports-export/publications_report-'.$from.'.csv', 'w');
fputcsv($fp, $header);
fputcsv($fp, array(date('M d, Y',strtotime($data_print->pfile_date))));
fclose($fp);
$file = 'reports-export/publications/publications_report-'.$from.'.csv';
if (file_exists($file)) {
//set appropriate headers
header('Content-Description: File Transfer');
header('Content-Type: application/csv');
header('Content-Disposition: attachment; filename='.basename($file));
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
ob_clean();
flush();
//read the file from disk and output the content.
readfile($file);
Related
My situation (all in PHP):
What I get: A Base64 encoded string from an API.
What I want: A link to click on that downloads this document as an PDF. (I am sure it will always be a PDF file)
I have tried this:
$decoded = base64_decode($base64);
file_put_contents('invoice.pdf', $decoded);
but I am kind off lost, can't seem to find a way to download it after decoding it.
I hope someone can help me, thanks in advance!
The example here seems helpful: http://php.net/manual/en/function.readfile.php
In your case:
<?php
$decoded = base64_decode($base64);
$file = 'invoice.pdf';
file_put_contents($file, $decoded);
if (file_exists($file)) {
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="'.basename($file).'"');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
readfile($file);
exit;
}
?>
This should force the download to occur.
Don't need to decode base64. You can send header with binary file.
header('Content-Description: File Transfer');
header('Content-Type: application/pdf');
header('Content-Disposition: attachment; filename='.$filename);
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . strlen($filedata));
ob_clean();
flush();
echo $filedata;
exit;
I have a code:
$name = "jeffrey.epub"; //for naming only
$file = "test/cover/cabang.epub"; //the real file to be downloaded
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="'.basename($name).'"');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
readfile($file);
but upon having the download file, it came out corrupted.i also change the header('Content-Type: application/octet-stream'); to header('Content-Type: application/epub+zip'); but still it was corrupted, i was wondering what is the problem that the file is being corrupted
I want download file that has UTF-8 filename, such as یک.jpg, in PHP.
I tested many solutions that are posted on websites but they are not working.
This is the solution I tested:
$file_utf8 = iconv( "iso-8859-1", "utf-8", $file );
if (file_exists($file_utf8))
{
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($file));
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
readfile($file);
exit;
}
else
echo($file_utf8);*/
and ...
So $fmain is equal to the file path, the and the randomly generated file name, it then fwrites 100 lines of html literal text (Using the ' '). That's great and all, but it then saves to the server, which is fine, but then it downloads to the client (the one that requested the file download) and it's blank. Does anyone have any ideas? This is all on the same page by the way. The file is complete on the server itself, but not when it's downloaded to the client.
fwrite($file, $line98 );
fwrite($file, $numberNewline);
fwrite($file, $line99 );
fwrite($file, $numberNewline);
fwrite($file, $line100 );
fwrite($file, $numberNewline);
fclose($file);
if (file_exists($fmain)) {
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($fmain));
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($fmain));
ob_clean();
flush();
exit;
}
Your script is just sending the headers, you should readfile also before ob_clean(). For instance it could be:
if (file_exists($fmain)) {
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($fmain));
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($fmain));
readfile($fmain);
ob_clean();
flush();
exit;
}
I have 2 files - 1.csv and 2.csv. I can download 1.csv with PHP by known approach:
$file = '1.csv';
header('Content-Description: File Transfer');
header('Content-Type: application/jpeg');
header('Content-Disposition: attachment; filename='.basename($file));
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: public');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
ob_clean();
flush();
readfile($file);
But my purpose is to have in downloaded file content of both files - 1.csv and 2.csv. Does anybody have some ideas how to implement this?
Thank you in advance.
If I understand you correctly this should do it.
$file1 = '1.csv';
$file2 = '2.csv';
header('Content-Description: File Transfer');
header('Content-Type: text/comma-separated-values');
header('Content-Disposition: attachment; filename='.basename($file1, '.csv') . '-' . basename($file2, '.csv') . '.csv');
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: public');
header('Pragma: public');
header('Content-Length: ' . (filesize($file1) + filesize($file2)));
ob_clean();
flush();
readfile($file1);
readfile($file2);
Consider using a loop for multiple files and mind the change of the Content-Type header for CSV files.
$file1 = '1.csv';
$file2 = '2.csv';
header('Content-Description: File Transfer');
header('Content-Type: application/jpeg');
header('Content-Disposition: attachment; filename="1-2.csv"');
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: public');
header('Pragma: public');
header('Content-Length: ' . (filesize($file1)+filesize($file2)));
ob_clean();
flush();
echo file_get_contents($file1).file_get_contents($file2);
Downloads the concatenation of the 2 file contents