-1

I am using this code to write pdf file to the server. It sends the file to a folder called DATA. And I am storing the file name to mysql.

$target = "data/";  $target = $target . basename( $_FILES['file']['name']); 

if(isset($_POST['Submit']))
{
{
$path = $_FILES['file']['name'];
}  

if(move_uploaded_file($_FILES['file']['tmp_name'], $target))
{
echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded;  
}
else
{   
echo "Sorry, there was a problem uploading your file.";
} 

Now plz can anyone help me to download this pdf file.??

  • http://stackoverflow.com/a/17866898/151097 – Rufinus Sep 09 '14 at 10:39
  • 1
    without seeing your database function that stores the info it is not possible to say how to retrieve it. – RST Sep 09 '14 at 10:42
  • Just echo the path to the file as a link? – Traian Tatic Sep 09 '14 at 10:53
  • @TraianTatic.. plz can you give me an example?? – Chandresh singh Sep 09 '14 at 10:57
  • 1
    @Chandreshsingh Accessing a .pdf file directly in the browser (here's an [example](http://www.rebeccalouiselaw.com/wp-content/uploads/2014/08/example_pdf.pdf)) allows you to view it and, by rightclicking, to download it. This is the most simple and trivial way of doing what you want. So all you have to do is `echo 'Download PDF;` , considering that `$file_name` is the value that you stored in your database and that you actually have the knowledge to do this. – Traian Tatic Sep 09 '14 at 11:05

2 Answers2

0

If you are able to upload and store the name of PDF in mysql then you will need to query the db and build the path for PDF and send download header to force it download in browser

below is some code to help you

HTML

create dynamic link for each row its seems Tno is primary key so am passing in download link as file param

<a href="download.php?file=Tno">Download</a>

PHP

//Get the file id form $_GET['file'];
$id = $_GET['file'];

//Get the row from db
$sql = "SELECT file FROM tenders WHERE Tno = $id";

//Build the path 
$file = "data/" . $mysql_row['file '];

header("Content-type:application/pdf");
// It will be called downloaded.pdf
header("Content-Disposition:attachment;filename='downloaded.pdf'");
// The PDF source
readfile($file);

Make sure you dont echo anything before this code

Saqueib
  • 3,484
  • 3
  • 33
  • 56
  • plz explain a little bit... This code needs to be in a new .php file? Or on the page where i have given a download button? – Chandresh singh Sep 09 '14 at 10:47
  • in a new page will be good like `download.php`. if you can show us the table column name of pdf i can help you better – Saqueib Sep 09 '14 at 10:49
  • INSERT INTO tenders(Tno,Tid,file)values('$Tno','$Tid','$path'); Column- "file" stores the name of the pdf file which is collected in the variable $path. – Chandresh singh Sep 09 '14 at 10:52
  • and how you are making download link like this `Download` or post what you have done – Saqueib Sep 09 '14 at 10:59
  • thanks for the efforts bro... actually previously i was string the whole doc as blob form but then i dropped the idea.. And I am new to this concept of storing the path name in the DB..But anyhow I have done the 'half' work of storing the pathname.. But now I have no Idea how to download the pdf.. :( Plz suggest something.. – Chandresh singh Sep 09 '14 at 11:03
  • updated the answer, you need to get the row using your DB access script – Saqueib Sep 09 '14 at 11:12
0
   // link for download :
        <a href="download.php"> download </a>

  //  php code inside download.php
        <?php
        //download.php
        $filename="sample.pdf"; // YOUR File name retrive from database
        $file= "data/".$filename; // YOUR Root path for pdf folder storage
        $len = filesize($file); // Calculate File Size
        ob_clean();
        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"); // Send type of file
        $header="Content-Disposition: attachment; filename=$filename;"; // Send File Name
        header($header );
        header("Content-Transfer-Encoding: binary");
        header("Content-Length: ".$len); // Send File Size
        @readfile($file);
        exit;

        ?>
coDe murDerer
  • 1,858
  • 4
  • 20
  • 28