How to download a PDF file instead showing it in Adobe Reader? - php

I'm trying to create a download link for a PDF file. It works fine if Adobe Reader is installed, but if I uninstall it, it tries to open in browser and fails.
Please tell me what is the actual problem about this?
Thanks in Advance.

The problem is that when adobe reader is installed than it automaitcally gets the header of .pdf file and show the file.
You can use this.It will always prompt to download the file ..
$path="uploads/";
$actualfilename=$path.$row["name"];
//name of the file or location of file..
if($typeofview=="download") {
#readfile($actualfilename);
header('Content-type: application/pdf');
header('Content-Disposition: attachment; filename="' . $actualfilename. '"');
header('Content-Transfer-Encoding: binary');
header('Content-Length: ' . filesize($actualfilename));
header('Accept-Ranges: bytes');
exit;
}

This is a duplicate of this question but here is the answer:
$path_to_file = '/var/www/somefile.pdf'; //Path to file you want downloaded
$file_name = "somefile.pdf"; //Name of file for download
header('Pragma: public'); // required
header('Expires: 0'); // no cache
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header("Content-Type: application/octet-stream");
header('Content-Disposition: attachment; filename="'.$file_name.'"');
header('Content-Transfer-Encoding: binary');
readfile($path_to_file);
die();

Related

PHP force download base64image

I want to offer a PNG image to download only for some users, and the image should not be located anywhere, so the users should not see it's path.
That's why I inserted it to the download.php as a base64 image. The problem is, however the image is offered for download, but it shows 0 B size when downloaded.
$base64strImg = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAxoAAARjCAIAAABnqMWCAAAACXBIWXM...';
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename=myimage.png');
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
ob_clean();
flush();
readfile(base64_decode($base64strImg));
exit;
Not sure where is the problem, if it can't handle big image or why it can't be downloaded.
I also tried this way:
header('Content-Disposition: attachment;filename="test.png"');
header('Content-Type: application/force-download');
echo base64_decode($base64strImg);
Now the image has correct size, but still, can't be opened after download. The image viewer says unsupported format.
And third option - without decoding, it also has a correct size, but still can't be opened.
header('Content-Description: File Transfer');
header('Content-Type: image/png');
header('Content-Disposition: attachment; filename=test.png');
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . strlen($base64strImg));
ob_clean();
flush();
echo $base64strImg;
exit;
According to advice from #Lawrence I updated my code and this way it works:
$resultimg = str_replace("data:image/png;base64,","",$base64strImg);
header('Content-Disposition: attachment;filename="test.png"');
header('Content-Type: image/png');
echo base64_decode($resultimg);
you don't have to do all that you can just upload image and restrict access to it by htaccess or permissions and use readfile with the headers in download.php and check if the user has the permission to download the file .

change the name of the downloading file for the user downloading in php or html

Hi Is there is any way which i can give the user different name of the file which i have inside my server
for example i have file inside my server which has md5 name like
33e65007ba9387583d4902e5497fc9ac.mp3
i need when the user click to download this file to change the name of the downloading file to something.mp3
and this changing wanna be just with the user file downloading and it will not effect the name of the file in my server
More detail :
name of server file is :33e65007ba9387583d4902e5497fc9ac.mp3
name of the user file after downloading something.mp3 with
out changing server file
how can i do this thing ? with php ?
You could store file name in $name, and file content in $content. Then, use the following codes to download the file:
header('Content-Description: File Transfer');
header("Content-type: application/force-download");
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename=' . $name);
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: ' . strlen($content));
ob_clean();
flush();
echo $content;
i solved the problem by changing my download php file to
<?php
$file = $_GET['file'];
header ("Content-type: octet/stream");
header ("Content-disposition: attachment; filename=".$file.";");
header("Content-Length: ".filesize($file));
readfile($file);
exit;
?>

Php force Download - downloading corrupted file

Hi I am trying to allow download a demo file which is related to a product. my code is like this
if(isset($_POST['submit'])){
$root = $_SERVER['DOCUMENT_ROOT'];
$path = "/download/";
$path = $root.$path;
$filename = $demo;
$file = $path.$filename;
header('Content-Type: application/force-download');
header("Content-Transfer-Encoding: Binary");
header("Content-Disposition: attachment; filename=\"".basename($file)."\";" );
header("Content-Length: ".filesize($file));
readfile($file);
}
I am able to get file name which is associated with the product by
$demo
After user submits his information, download will start automatically. Now this is downloading a non existing file or corrupted file with the proper file name. please help
<?php
$file = 'monkey.gif';
if (file_exists($file)) {
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
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;
}
?>
As you can see Content type is application/octet-steam meaning file is byte by byte encoded. Also the cache headers are set. Then headers are forcefully sent by ob_clean();flush(); and then the file is read.
The file_exists is there to ensure that given file exists. You should also try not not thrust user input as they could easy write names for your php codes and download EACH file. And with ../ in names of the files, even your documents or system files and so on.

php - Filename of file when using firefox to download

I see this tutorial to make download file but I have a problem
Here is my example
$file_url = "D:/my file name.doc"
header('Content-Type: text/json; charset=UTF-8;');
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename=' . basename($file_url)."");
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_url));
ob_clean();
flush();
readfile($file_url);
Everything's well in ie or chrome. But when I using firefox to download file. The file download has my is the file name? How to fix that thanks
header('Content-Disposition: attachment; filename="' .basename($file_url).'"');
First of all you are missing ; on the end of the first line.
I think the problem is in filename with spaces.
Try to use readfile(urlencode($file_url)); instead of readfile($file_url);
You should quote filename.
header('Content-Disposition: attachment; filename="' . basename($file_url) . '"');
EDIT: It was as selected answer just with typo in it. Now I fixed it.

PHP download script corrupts the file

what could be a problem of that, file int the server is good, but after I press download it get corrupted...
<?php
if (isset($_POST['submit'])) {
header('Content-type: application/vnd.openxmlformats-officedocument.wordprocessingml.document');
header('Content-Disposition: attachment; filename="ataskaita.docx"');
readfile('../generavimui/ataskaita.docx');
}
?>
Look into the file using notepad or a hex editor. There probably is a PHP error message in there.
Possible reasons include
The file you are looking for doesn't exist
$_POST['submit'] is not set
try ereasing the output buffer before reading the file
if (isset($_POST['submit'])) {
header('Content-type: application/vnd.openxmlformats-officedocument.wordprocessingml.document');
header('Content-Disposition: attachment; filename="ataskaita.docx"');
ob_clean();
flush();
readfile('../generavimui/ataskaita.docx');
}
I had the same problem that you are having, except I had some additional problems, such as trailing source code being included in the downloaded file.
To fix it, replace your code with this:
header('Content-Description: File Transfer');
header('Content-type: application/vnd.openxmlformats-officedocument.wordprocessingml.document');
header('Content-Disposition: attachment; filename="ataskaita.docx"');
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));
Source Site
Hope this helps

Categories