Download pdf from href link issue - php

I am working on a wordpress. I make a meta field for upload pdf file,pdf file upload sucessfully. Now i call meta value in href link for download the pdf file.But the problem is that it can open the file but not download.For this i search a code that is
<?php
header("Content-Disposition: attachment; filename=$event_flyer");
?>
$event_flyer is used for accessing meta value.
but its not working for specific href its just working for whole page on refresh.

If you want your pdf file to be downloadable by anyone who could browse your website just by clicking on links, then you can use the download attribute (for tags) which allows you to specify the linked resource :
Doc here : http://www.w3schools.com/tags/att_a_download.asp

Actually if you have a reader installed it will always open instead of downloading.
If you have hosted your wordpress on a live server then you can try to open link on a PC where PDF reader is not installed. It will download the file instead of opening.

You need more code, check this:
header("Content-disposition: attachment; filename=$pathtopdf");
header("Content-type: application/octet-stream");
readfile($pathtopdf);

Related

How can i suggest a file name on file download

i have a webpage with a url going out.
I want to change the filename that the person will receive.
test - click here to download
<?php
header('Content-Disposition: attachment; filename="test.avi"');
?>
The problem is:
I receive the web page and not the file..
When i access the page, a download starts directly.. it's should wait for me to click on the "click here to download".
Also, the downloaded file, is only the actual source code of the page and not the file that i want to download..
Here is a screenshoot, better to explain than my words with my bad english :D
http://i.stack.imgur.com/Si9cP.png
Thanks.
It very simple.
You just need to add download attr to your tag like this:
<a href="http://downloadlink.com/Grizzly.avi" download>test - click here to download</a>
It will start downloading.
For reference
The Content-Disposition needs to go in the header for the file you are offering the user to download (i.e. the avi file in your example).
It doesn't go in the header for the HTML document with the link to the file.

How to view the uploaded(.doc) file in a div?

I uploaded the .doc file using php code and saved it in a folder ,the path is stored in database.
When i tried to make a view of the doc file in a div ...a dialog box appears asking whether to save or openwith ..
I'm using wampserver ....I just tried like this
<iframe name="awindow" frameborder=2 width=580 height=440 src="www/siva_example/pdf/1_siva.doc"></iframe>
Any help regarding..
Is it possible to view .doc file in browser or i have to convert it to pdf format...
It's not possible to display a .doc[x] in HTML. You can try to convert the .doc[x] to HTML or to an image.
you can view doc file by adding this to your header at request time
<?php
header('Content-disposition: inline');
header('Content-type: application/msword');
// not sure if this is the correct MIME type
readfile('MyWordDocument.doc');
exit;
You could try using google docs for this, use the below code, it works perfectly for ppts, have not been able to test it for a doc yet
<iframe src="http://docs.google.com/gview?url=http://yourdomain.com/document.doc&embedded=true" style="width:550px; height:450px;" frameborder="0"></iframe>
Ofcourse you would have to update the url as per your program using PHP

file download through php code

i've created a php file which shows data from database:
http://www.eedmoe.gov.bd/report/app/webroot/files/uploaded/tenders/tender.php
But I want to download files. Report.doc, contact.txt, etc files are saving this folder:
http://www.eedmoe.gov.bd/report/app/webroot/files/uploaded/tenders/
I want that any visitor or user can download those files by clicking or save as. What should be the php function or code am I going to use?
Change the href attribute of the links to the filenames. To download 'contact.txt' for example use this link in your html:
contact.txt
You'll need no php for this.
You can also make a specific page to force download if you don't want it to be opened by the browser :
<?php header("Content-Type: application/force-download"); ?>
See Doc here

Opening PDF inline in a frame - how to set the file name?

While rendering a PDF inside a frame in Firefox inline, the file name is ignored (click on save as after the PDF opens, and the name of the frame is shown in the save box, instead of the file name). I tried header('Content-Disposition: inline; filename=abc.pdf'); - it doesn't work. The same code works for attachment though, just doesn't work for inline. How to fix this?
This is in PHP, FF 15 and on windows.
You cannot set a file name while embedding the PDF in a frame or a browser tab
you change the title of the frame
you make another button for download where you force the user to download with your custom name

Open my php page as PDF

I have a button on my page that links to the report.php page. What I want is that instead of clicking the button and see the php page, to click the button and open this report.php page as a PDF one.
Is this possible? How can I do it?
You can generate PDF and allow user to download it (if browser supports inline PDF display it will look as you want). Try mPDF. It generates PDFs from HTML and CSS.
Ofc its possible, you define headers and make a recommended file name istead of the extension php
<?php
// We'll be outputting a PDF
header('Content-type: application/pdf');
// It will be called downloaded.pdf
header('Content-Disposition: attachment; filename="downloaded.pdf"');
// The PDF source is in original.pdf
readfile('original.pdf');
?>
first example found on php.net
http://php.net/manual/en/function.header.php
assuming of course that the file your generating is compatible with pdf, you can find alot of pdf generators online but i would recommend using http://phpclasses.org
You can try this, HTML to PDF Converter. Convert your HTML to PDF and then display on screen, Here are examples.

Categories