Open new tab pdf in php - php

How do I put a link to open a pdf in pdf?
I have a code below which does not work
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));
ob_clean();
flush();
readfile($file);
exit;
It just shows the link to download and does not generate my file to download.

Related

Downloading PDF File always damaged

i want to create a download pdf file, but its always damaged
i had try this code in other function its working correctly but in this function the downloaded file is corrupt
source and downloaded file has same size its 3.899
this is my code before :
if (file_exists($dir)) {
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="'.basename($dir).'"');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($dir));
readfile($dir);
exit;
}
and i had try this code too for this issue :
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="' . basename($file) . '"');
header('Content-Transfer-Encoding: binary');
header('Connection: Keep-Alive');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . $length);
set_time_limit(0);
readfile($file);
exit;
i think the problem is in file size,
my code is work corretly in size under 1Mb but i dont know how to change max download size, i had try to search in php.ini but i cant find
====> update
i had try to change file to smaller size file (below 1Mb) but its still damaged

epub corrupt when successfully downloaded

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

Export xlsx file using php

I need to download a file in XLS & XLSX format, I tried Many Way but it is not working.
My download code is as follows:
$file = "Reports.xlsx";
header('Content-Description: File Transfer');
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment; filename='.basename($file));
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
readfile($file);
Both i Tried :
header('Content-type: application/octet-stream'); - not working
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'); - not working
For Xls:
header("Content-Type: application/vnd.ms-excel; charset=UTF-8;"); - Working for Xls

PHP download gives me empty pdf

$file = "http://www.abax.se/content/download/137/862/file/example.pdf";
header('Content-Description: File Transfer');
header('Content-Type: application/pdf');
header('Content-Disposition: attachment; filename='.basename($file));
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
flush();
readfile($file);
This is the code I'm running, I'm out of ideas, any solution?
Remove header('Content-Length: ' . filesize($file));.
Thats what's breaking your output.
filesize() expects a local file, not an url.
Working code:
<?php
$file = "http://www.abax.se/content/download/137/862/file/example.pdf";
header('Content-Description: File Transfer');
header('Content-Type: application/pdf');
header('Content-Disposition: attachment; filename='.basename($file));
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
flush();
readfile($file);
?>

Download content of several files on server with PHP

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

Categories