display pdf in browser using php - php

hi so im trying to display a pdf file in the browser using php so i used this code
<?php
$filename = "uploads/docs/Preamble and National Territory.pdf";
header("Content-type: application/pdf");
header("Content-Length: " . filesize($filename));
readfile($filename);
exit;
?>
i juat hardcoded the pdf file name but will change it later my problem is it does not load the pdf file in my browser here is what it says
any idea how to improve my code? or what im doing wrong? thanks in advance

Related

Download image created by the PHP Exif Library from browser

Im using PEL - the PHP Exif Library to add metadata to my image.
the script work well and i can do online view my image with browser.
$jpeg = new PelJpeg($inputfile);
$jpeg->setExif($exif);
header("Content-Type: image/jpeg");
echo $jpeg->getBytes();
but when i tried to download it, script did not work.
$jpeg = new PelJpeg($inputfile);
$jpeg->setExif($exif);
header("Content-Type: image/jpeg");
header('Content-Disposition: attachment; filename=nature.jpg');
Please help.
Thank you!
im trying to allow user download the file from browser but the file i got has 0 byte and said corrupted.

Cant download a SWF image or preview it using PHP

Hi Stackoverflow community,
I've been trying to download a swf image from a extern server, which you can reach by a simple url.
Sadly the downloaded SWF wouldn't show up in the browser, so i assume its a broken file. Also I'm not able to preview the SWF file through PHP.
I know I could embed the SWF but i kinda need a screenshot or a downloaded version of it.
Code I've tested to download the SWF to my server:
function save_swf(){
$swf_file = file_get_contents('http://assets.zwinky.com/assets3/avatar/avatar10.8.swf?u=' . $this->username);
file_put_contents(strtolower($this->award . '_' . $this->username . '.swf'), $this->swf);
}
Code which i used to check if the image can be previewn by PHP:
<?php
$data = #file_get_contents('http://assets.zwinky.com/assets3/avatar/avatar10.swf?u=dane');
header("content-type: application/x-shockwave-flash");
header("Content-Disposition: inline;" echo $data);
header("accept-ranges: bytes", true);
header("connection: keep-alive", true);
echo $data;
?>
Does anyone have a idea why it wouldn't work?
Basis SWF im trying to get the file from: http://assets.zwinky.com/assets3/avatar/avatar10.8.swf?u=dane
If you write the data to a file locally on the server, the following should work:
// Write swf file locally using file_put_contents();
$fileName = ""; // The filepath where you've written it to
header("Content-Type: application/x-shockwave-flash",true);
header("Content-Length: {strlen($fileName)}",true);
header("Accept-Ranges: bytes",true);
header("Connection: keep-alive",true);
header("Content-Disposition: inline; filename=$fileName");
readfile($fileName);
Let me know if you need any addition help with this.

Force download file that's not placed on the server

I'm trying to force download a pdf file that I'm generating. I don't need the pdf file to be actually saved on the server.
So when I generate my pdf file, I get the file content. I then encode it with base64. Now the problem is that I need to force download it. I've looked all over the web, but I haven't found any search results that tells me how to do this without the file actually being placed on the site.
I've tried the following code:
header("Content-type: application/pdf");
header("Content-disposition: attachment; filename=\"invoice.pdf\"");
header("Content-Length: ".filesize($pdffile));
readfile(base64_decode($pdffile));
But, it's giving me a corrupt pdf file, (1 kb). The actual file should be around 50kb.
Any ideas, as to what I can try?
readfile trying to output content from file, but you have only data string. Try this instead:
header("Content-type: application/pdf");
header("Content-disposition: attachment; filename=\"invoice.pdf\"");
echo base64_decode($pdffile);
I also suggest rename $pdffile to $pdfcontent for even better clarification.

how to download the video using php script

In my program I want to add a download option to download the currently straming video. I tried this code:
$psp = "Tom_20_amp__20Jerry_20race-1.flv";
header("Content-type:application/octet-stream");
header("Content-Disposition:attachment;filename=$psp");
But I get this error:
"C:\DOCUME~1\ADMINI~1\LOCALS~1\Temp\Tom_20_amp__20Jerry_20race-1-5.flv" is not a valid FLV file.
the video streaming properly. please guide me
Change you're header from :
header("Content-type:application/octet-stream");
To :
header("Content-type: video/flv");
Then you can do :
header("Content-Disposition:attachment;filename=\"$psp\"");
//allways a good idea to let the browser know how much data to expect
header("Content-length: " . filesize($psp) . "\n\n");
echo file_get_contents($psp); //$psp should contain the full path to the video
If you are looking to allow files to be downloaded instead of streamed then something like this should work. You will obviously need to change the paths.
// We'll be outputting a PDF
header('Content-type: application/pdf');
// It will be called downloaded.pdf
header('Content-Disposition: attachment; filename="download.pdf"');
// The PDF source is in original.pdf
readfile('original.pdf');
EDIT
In your case it will be something like this.
// We'll be outputting a video
header('Content-type: video/flv');
// It will be called video.flv
header('Content-Disposition: attachment; filename="video.flv"');
// The PDF source is in original.flv
readfile('original.flv');

Showing generated PDF problem

I have a problem with showing generated PDF. The pdf is saved in dataBase. I'm continuing work of a guy that started work that way, so I need to work that way. There's no problem with saving the file on computer,it's working fine, but I need to show it in new window when it's read from dataBase...
And sorry about my English. :)
<?
$pdf=pdf_new();
pdf_open_file($pdf,"");
pdf_begin_page($pdf,600,800);
$font=pdf_findfont($pdf,'Helvetica-Bold','host',0);
pdf_setfont($pdf,$font,30.0);
pdf_show_xy($pdf,"Sample Text",50,600);
pd_set_parameter($pfd,"openaction","fitpage");
pdf_close($pdf);
$buf=pdf_get_buffer($pdf);
$len=strlen($buf);
header("Content-Type: application/pdf");
header("Content-Length: $len");
header("Content-Disposition: inline; filename=sample.pdf");
echo $buf;
pdf_delete($pdf);
?>
More information and some code example would be helpful.
You'll need to set the correct header before outputting to the window.
header('Content-type: application/pdf');

Categories