PHP download gives me empty pdf - 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');
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);
?>

Related

Open new tab pdf in 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.

PHP not download apk file correctly on my server

I must download an apk file from my server, but i have an incomplete download.
This is the code:
header('Content-Type: application/vnd.android.package-archive');
header('Content-Disposition: attachment; filename="Zappapp1.0.apk"');
readfile('Zappapp1.0.apk');
EDIT
I try this but it not work..
$file = 'zappapp.altervista.org/apk/Zappapp1.0.apk';
if (file_exists($file)) {
header('Content-Description: File Transfer');
header('Content-Type: application/vnd.android.package-archive');
header('Content-Disposition: attachment; filename='.basename($file));
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
ob_clean();
flush();
readfile($file);
exit;
}
Sorry for my english but i'm italian ..
You Can Try This Maybe Its Help you.............
$file = '/home/bla-bla/domains/bla-bla.com/file/file.apk'; //not public folder
if (file_exists($file)) {
header('Content-Description: File Transfer');
header('Content-Type: application/vnd.android.package-archive');
header('Content-Disposition: attachment; filename='.basename($file));
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
ob_clean();
flush();
readfile($file);
exit;
}

Downloaded file save with 0kb and damaged

I have a problem with download script
when I download any file it is save with 0kb.
Any ideas? Here is my PHP script
<?php
$file = $_GET['fname'];
$fullpath = get_bloginfo('template_url')."/application/display/1358947404.pdf";
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($fullpath));
header('Expires: 0');
header('Cache-Control: must-revalidate');
ob_clean();
flush();
readfile($fullpath);
?>
Please add content-length header as well .
Please try this:-
<?php
$file = $_GET['fname'];
$fullpath = get_bloginfo('template_url')."/application/display/1358947404.pdf";
$headers = get_headers($fullpath, 1);// use file absolute path here
$fsize = $headers['Content-Length'];
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($fullpath));
header('Expires: 0');
header('Cache-Control: must-revalidate');
header("Content-Length: " . $fsize);
ob_clean();
flush();
readfile($fullpath);
?>
Try below code hope it will help you.
I have tested this in my localhost.
$file = $_GET['fname'];
$fullpath = get_bloginfo('template_url')."/application/display/1358947404.pdf";
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($fullpath));
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . filesize($fullpath));
ob_clean();
flush();
readfile($fullpath);

Force Downloading file through php not working

I was trying to write a script through which user can download the image directly.
Here is the code i end up with,
<?php
$fileContents = file_get_contents('http://xxx.com/images/imageName.jpg');
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.urlencode("http://xxx.com/images/imageName.jpg"));
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($fileContents));
ob_clean();
flush();
echo $fileContents;
exit;
?>
But everytime i hit the url for the above script in the browser it returns a file with zero byte data.
would you like to help me to resolve this problem ?
Try the code below
<?php
$file_name = 'file.png';
$file_url = 'http://www.myremoteserver.com/' . $file_name;
header('Content-Type: application/octet-stream');
header("Content-Transfer-Encoding: Binary");
header("Content-disposition: attachment; filename=\"".$file_name."\"");
readfile($file_url);
?>
Read more , Read this tutorial too
I notice that you use filesize on the contents of the file instead of at the filename itself;
Your code would work if it was:
<?php
$filename = 'http://xxx.com/images/imageName.jpg';
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.urlencode($filename));
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($filename));
ob_clean(); // not necessary
flush(); // not necessary
echo file_get_contents($filename); // or just use readfile($filename);
exit;
?>

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