file download through php code - php

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

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

Download pdf from href link issue

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);

How to display most used file (pdf, doc, docx, xls, …) into a browser by using PHP?

I need to display a (pdf,doc,docx,txt) documents inside an php web form with the ability to select a part of the document content and do some processing on it in the right click (like saving the selected content to DB)?
I try this way :-
<iframe src="http://docs.google.com/viewer?url=<?=urlencode($docx)?>&embedded=true" width="600" height="780" style="border: none;"></iframe>
But is show me as html
PHP can set the header information for a file and so you can display anything as file.php, loading the file.php you can then specify what it should be handled as- for example you can generate an image and output it as a jpeg file :
header('Content-Type: image/jpeg');
$img = LoadJpeg('bogus.image');
imagejpeg($img);
imagedestroy($img);
from http://php.net/manual/en/function.imagecreatefromjpeg.php example.
Using a similar method for setting the header content-type you can output PDF contents or DOC or whatever, by setting the header to the correct file type and loading the contents of the file with PHPs file_get_contents() ( http://php.net/manual/en/function.file-get-contents.php ) function, and outputting that to the browser / iframe.
Two things you can do:
Just link to the .pdf/etc. location on your server
Save the image of it as a .jpg and display that then put a link to download the pdf/whatever

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