How to display .pdf files from mysql using php

Asked
Active3 hr before
Viewed126 times

8 Answers

usingfilesdisplay
90%

Suppose we have a 'infopdf' TABLE in the Database as follows. You can use your existing database or copy and paste this in MySQL.,Here is the PDF file displayed on the webpage that is fetched from the MySQL database table using the PHP code:,In this article, we have mentioned all about emojis. It's invention, world emoji day, emojicode programming language and much more...,Now, create a PHP file 'index.php' and write the database connection code at the top. For this, we are using Object Oriented PHP MySQLi connection code. Then, fetch the pdf file name and directory using select statement. To embed the PDF in a web page, we use the iframe element.

Suppose we have a 'infopdf' TABLE in the Database as follows. You can use your existing database or copy and paste this in MySQL.

CREATE TABLE IF NOT EXISTS `infopdf`(
   `fileid`
   int(11) NOT NULL AUTO_INCREMENT,
   `filename`
   varchar(150) NOT NULL,
   `directory`
   varchar(150) NOT NULL,
   `created_date`
   date NOT NULL,
   PRIMARY KEY(`fileid`)
)
load more v
88%

Dear All I'm able to display the images from mysql. But when I want to display the pdf that also stored in my database I'm not able to do that. When I use this code this code show a small green piece of image but not the .pdf files. This code well works on image . But I'm not able to show .pdf files. can any help me to tell me where in my code I have to cahnge . And I request please give the proper change line that is able to show PDF file. thanks in advance`,For displaying pdf file you have to set proper headers also to display the file. Try setting these headers and replace $filename with your file path.,Before displaying pdf file first check if its pdf then give path of pdf image from your directory,This is because you are adding image instead of pdf...there isn't any tag with <pdf like we have for image so create a clickable link.There is a different between pdf and image.

Your clickable link will be generated by

 $img="<a href='"$row[' image']."'>Download </a> ";

where you will create an iframe and open the pdf in iframe

 echo" <iframe src='"$row[' image']."'></iframe>";

row['image'] must have a complete path like

 http: //hostname.com/uploads/yourPDFfile.pdf
load more v
72%

The tutorial about web design, tutorial bootstrap, php code, web technology, learn french, android developer


REATE TABLE IF NOT EXISTS `filepdf`(
   `id`
   int(11) NOT NULL AUTO_INCREMENT,
   `judul`
   varchar(255) NOT NULL,
   `jenis`
   enum('Peraturan Pemerintah', 'Peraturan Presiden', 'Keputusan Menteri') NOT NULL,
   `namafile`
   varchar(100) NOT NULL,
   PRIMARY KEY(`id`)
) ENGINE = InnoDB DEFAULT CHARSET = latin1 AUTO_INCREMENT = 2;

INSERT INTO `filepdf`(`id`, `judul`, `jenis`, `namafile`) VALUES(1, 'KAWASAN EKONOMI KHUSUS TANJUNG KELAYANG', 'Peraturan Pemerintah', 'kawasanekonomi');
load more v
65%

If you recall I mentioned the 2 template file variables ‘$title’ and ‘$output’. Both of them are set to values in this file and then passed to the base template file at this line: ,File base.html.php is the foundation template file. Data is sent – or included – in other template files, then rendered in the browser from base.html.php using 2 variables: ‘$title’ and ‘$output’. ,The ‘pdf_project’ table data is then rendered in the browser using the ‘$title’ and ‘$output’ variables through these 3 lines of PHP code: ,Provide a list of all the current available .pdfs and Project Names (optional)

In Use MySQL BLOB column with PHP to store .pdf file, I covered an example of how to store a .pdf file in the actual database table using the MySQL BLOB datatype and PHP. Now that we have .pdf’s stored in the database, how do we display them in the browser? This blog post answers that exact question. Continue reading to see a working example using PHP…

BLOB
load more v
75%

Example 1: This example display the pdf file on the browser.,How to open a PDF files in web browser using PHP?,How to convert an array to CSV file in PHP ?,Example 2:this examples displays a format and explains every section of code

40%

PDFs are: application/pdf - so in PHP you would use header(“Content-Type: application/pdf”); You may have to futz around with it to get it to work though… you always do…,Without wanting to sound condesending - but do you have a PDF reader on the machine you wish to open them on like Adobe Acrobat Reader? ,Thanks for the help. I ve had a look and the filetype is application/pdf but it still doesn’t lauch acrobat when i try and open it. I’ll have a play around and see what happens. Cheers,Regardless - storing it either way - you should be able to download the document in the appropriate format. Make sure the right mime type is beng sent with the link-click.

Hi thanks for the replies, Iam very new to php and mysql so i may have misunderstood what i can actually do with mysql as i was going to store the pdf as binary data within the mysql table. Ive tried it with some word files and that works fine. Is this something that shouldn’t be done?. It would make it easier for people in my office to add documents they have done to the website. I do have acrobat reader working on my machine (i don’t blame you for asking its always worth checking the simple things first). The display page i am using is below. Thanks for you help.

< ? php

if ($id) {

   @MYSQL_CONNECT("abc", "def", "ghi");

   @mysql_select_db("DB1");

   $query = "select bin_data,filetype from binary_data where id=$id";
   $result = @MYSQL_QUERY($query);

   $data = @MYSQL_RESULT($result, 0, "bin_data");
   $type = @MYSQL_RESULT($result, 0, "filetype");

   Header("Content-type: $type");
   echo $data;

}; ?
>
22%

Search Answer Titles

<html>
<head>
<title>  FORM </title>
</head>
<body align="left">
<h1> FILE UPLOAD </h1>

<form action = "term5b.php" method = "POST" enctype="multipart/form-data"/>

    <input type = "file" name = "fileupload"/></br>  
    <input type = "submit" name = "opt" value = "upload"/></br> </br>  

</form>
</body>
</html>







<?php
   $target_dir="E:\ ";
   $filename=$_FILES["fileupload"]["name"];

   $tmpname=$_FILES["fileupload"]["tmp_name"];
   $filetype=$_FILES["fileupload"]["type"];
   $errors=[];
   $fileextensions=["pdf"];
	$arr=explode(".",$filename);
   $ext=strtolower(end($arr));

   $uploadpath=$target_dir.basename($filename);
if(! in_array($ext,$fileextensions))
   {
     $errors[]="Invalid filename";
   }
   if(empty($errors))
   {
     if(move_uploaded_file($tmpname,$uploadpath))
     {
       echo "file uploaded successfully";
     }
     else
     {
       echo "not successfull";
     }
   }
   else
   {
      foreach($errors as $value)
      {
         echo "$value";
      }
   }
?>
60%

Hi, Im working on project right now in webdevelopment. im trying to view a PDF file in a browser which is the filepath was stored in database and the actual file save was in folder inside my webfolder and could retrieve/view by its ID. here's some of my code, How to filter data from a MySQL Database Table with PHP 8 , How to store array elements in MySQL database. in a single column 9 ,My issue here, when i run/compile it, it says "Failed to load PDF document". what am i missing here? do you have any suggestions, tutorial link?

Hi,
Im working on project right now in webdevelopment. im trying to view a PDF file in a browser which is the filepath was stored in database and the actual file save was in folder inside my webfolder and could retrieve/view by its ID. here's some of my code

< ? php

if (isset($_POST['search'])) {

   $con = new PDO("mysql:host=localhost;dbname=qfms", "root", "");
   $con - > setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); //setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

   $fid = $_POST['id'];
   $stmt = $con - > prepare("SELECT filename FROM rabbit_tbl WHERE id= '".$fid.
      "'");

   $stmt - > execute();

   while ($row = $stmt - > fetch()) {
      header('Content-type: application/pdf');
      header("Content-Disposition: inline");
      echo "/pdffiles".$row['id'].
      ".pdf");
   readfile('pdffiles/');
}
} else {
   echo "Enter id";
}

?
>

Other "using-files" queries related to "How to display .pdf files from mysql using php"