PHP Unable to write the content in PDF - php

After executing the below code, Browser automatically Downloading PDF Document. When i open that PDF, i am getting below Error.
I got below snippet idea from source
<?php
$filename = "yourfilename.pdf";
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-Type: application/pdf");
header("Content-Disposition: attachment; filename=$filename");
header("Content-Transfer-Encoding: binary");
echo "my contents should display in PDF Document";
?>

Related

Download file .ZIP with PHP automatically

I would like to download a file from a folder on my server and automatically start the download without the person seeing the original file link.
Here is my current script, but it downloads an invalid corrupted file.
$filename = "dsk.zip";
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename="./zz/'.$filename.'");
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".filesize("./zz/".$filename));
ob_end_flush();
#readfile("./zz/".$filename);
Can you please help me? The file, dsk.zip, is in the folder zz.
$file = "dsk.zip"; // this is what you will get from a param (i.e. ?file=dsk.zip) or from DB query
header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-Disposition: attachment; filename='zz/'.$file");
header("Content-Length: ".filesize($file));
header("Content-Type: application/force-download");
header("Content-Transfer-Encoding: binary");
readfile('zz/'.$file);

Pseudo Streaming MP4 with php

i am trying to make the video seek using php file
this is my code
<?php
$path = 'http://exmaple.com/video.mp4';
$mm_type="video/mp4";
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-Type: " . $mm_type);
header("Content-Length: " .(string)(filesize($path)) );
header('Content-Disposition: attachment; filename="'.basename($path).'"');
header("Content-Transfer-Encoding: binary
");
readfile($path); // outputs the content of the file
exit();
?>
the video work but it is not possible to seek
Seeking requires that your server support handling of the Content-Range: header

PDF file not shownig on mozila but work fine in chrome .

I am using this code to download pdf file . its work fine in chrome but in mozila it create .htm file .Any can help me about this
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private", false);
header("Content-Type: application/pdf");
header("Content-Description: File Transfer");
header("Content-Disposition: attachment; filename=\"$filename\"");
header("Content-Transfer-Encoding: binary");
readfile('phpToPDF/examples/pdf/'.$filename);
you need to terminate current script. that's why you are getting .htm extension on Mozilla.
use exit(); at the end of your code.
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private", false);
header("Content-Type: application/pdf");
header("Content-Description: File Transfer");
header("Content-Disposition: attachment; filename=\"$filename\"");
header("Content-Transfer-Encoding: binary");
readfile('phpToPDF/examples/pdf/'.$filename);
exit();

Downloading A File Located Above public_html

I have a document hosted in a folder above public_html for security reasons - moving the files into the web root is not an option.
I'm trying to access them via a readfile() but whatever I download winds up being 0 bytes and an empty file. I think the problem is that the "Content-Length" header isn't getting a correct value because filesize($target) is giving no output.
Any thoughts? Code included:
header('Content-Description: File Transfer');
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Content-Transfer-Encoding: binary");
header("Pragma: public");
$file="../docs/$c/$path";
$size=filesize($file); //i think this line is the problem
header ("Content-Length: $size");
header("Content-Type: application/octet-stream");
header("Content-Disposition: attachment; filename=\"$filename\"");
readfile($file_url);
I don't see $file_url defined in your code. Maybe it should be $file:
header('Content-Description: File Transfer');
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Content-Transfer-Encoding: binary");
header("Pragma: public");
$file="../docs/$c/$path";
$size=filesize($file); //i think this line is the problem
header ("Content-Length: $size");
header("Content-Type: application/octet-stream");
header("Content-Disposition: attachment; filename=\"$filename\"");
readfile($file);
I also don't see $filename defined.

Get content of a zip file and echo it to user

I wanna make a link for download and all of my file is zip but idont want to my user knows where is my download link
so i wanna get content of zip file and echo it with zip header to user can download it how can i do this?
You should use a "download.php?get=". base64_encode($filename)
When the user click on "download zip", you redirect him with header("Location: download.php?get=". base64_encode($url) to that page.
The headers, here we go
$filename = base64_decode($_GET[get]);
$filepath = "/var/www/domain/httpdocs/download/path/";
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename=\"".$filename."\"");
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".filesize($filepath.$filename));
ob_end_flush();
#readfile($filepath.$filename);
Instead of experimenting with headers you could try downloading some 'ready for use' php class. Then your could do something like:
$fileDown = new PHPFileDownload(null, null, 'myFileName', null, 'mypath/myfile.txt');
$fileDown->exportData();

Categories