Default Image

Months format

Show More Text

Load More

Related Posts Widget

Article Navigation

Contact Us Form


Sorry, the page you were looking for in this blog does not exist. Back Home

How To View An Uploaded PDF File In PHP?


Uploading a PDF file in PHP is as easy as adding a couple of scripts. But what if you want to view the uploaded PDF file in PHP? 

Filestack is an option for you. With Filestack’s advanced features, viewing an uploaded PDF file in PHP with a PDF viewer couple of scripts is easy.

Before diving into Filestack’s dedicated document viewer, first, we’ll explore the basics of a PHP file uploader and the methods that enable us to view uploaded PDF files in PHP.

A PHP script with an HTML form allows users to upload files to the server. Initially, files are uploaded into a temporary directory and relocated to a target destination by a PHP script.

The phpinfo.php page describes the temporary directory used for file uploads as upload_tmp_dir, and the maximum permitted size of files that can upload is stated as upload_max_filesize. These parameters are set into the PHP configuration file php.ini

You can upload files from a local disk or URL by the following guideline from the local disk and URL, respectively.

    What Are The Supported Files To View An Uploaded File In PHP?

    PDF files (PDF)

    Powerpoint files (PPT, PPTX)

    Excel files (XLS, XLSX)

    Word files (DOC, DOCX)

    ODF word processing documents (ODT)

    ODF presentation documents (ODP)

    Image files (gif, tiff, jpg, jpeg, png)

    HTML files

    Plain text files and

    Adobe Illustrator and Adobe Photoshop files (AI, PSD)

    What Are The Options To View An Uploaded PDF File In PHP?

    PDF is one of the trusted document formats. While PHP provides simple functionality to embed PDF files, it also has simple implementation.

    You can find tons of DIY uploaders, but very few have the dedicated document viewer to manage and professionally view uploaded PDF files in PHP. You can preview documents and files within your application and website using Filestack’s advanced Processing Engine.

    Here, we’ll show two ways to display uploaded PDFs on a web page. It mainly depends on what you want to show. 

    For example, if you’re going to include a PDF file as a subpart of a web page, use an iframe as described in Method 1. But if you want to make the PDF as entire web content, use the header() function as mentioned in Method 2.

    Method #1 – Subpart of a Web Page

    If we have an ‘infopdf’ TABLE in the database like below, use your existing database or copy and paste this into MySQL.

    `fileid` int(11) NOT NULL AUTO_INCREMENT,
    `filename` varchar(150) NOT NULL,
    `directory` varchar(150) NOT NULL,
    `created_date` date NOT NULL,
    KEY (`fileid`)
    INSERT INTO `infopdf` (`fileid`, `filename`, `directory`, `created_date`) VALUES
    1, 'etp.pdf', '/document/', '2019-07-12');

    You have to create a PHP file, ‘index.php,’ and write the database connection code at the top. 

    To make this, we are using Object-Oriented PHP MySQLi-connection code. After that, fetch the pdf file name and directory using the select statement. We use the iframe element to embed the PDF on a web page.

    // Database Connection
    $conn =
    new mysqli('hostname', 'username', 'password', 'database');
    //Check for connection error
    $select =
    "SELECT * FROM `infopdf`";
    $result = $conn->query($select);
    while($row = $result->fetch_object()){
    $pdf = $row->filename;
    $path = $row->directory;
    $date = $row->created_date;

    echo '<h1>Here is the information PDF</h1>';
    echo '<strong>Created Date : </strong>'.$date;
    echo '<strong>File Name : </strong>'.$pdf;
    <iframe src=
    "<?php echo $path.$pdf; ?>" width="90%" height="500px">

    Method #2 – PDF as Entire Web Content

    When we want to show a PDF in addition to the web contents, we embed a PDF in an iframe like the above method.

    But when we want to display PDF as the entire content of a web page, it’s better to use the header() function. 

    In the next example, we have added the header() function. Additionally, we have set the file to ‘Content-type’, ‘Content-Disposition’, and ‘Content-Transfer-Encoding’ respectively. 

    We have used the PHP predefined readfile() function to read the file. We have also added an error control operator (@) in front of it, where this operator pretends to be an expression. But if there is any error or warning that this expression generates, we will ignore that.

    // Database Connection
    $conn =
    new mysqli('hostname', 'username', 'password', 'database');
    //Check for connection error
    $select =
    "SELECT * FROM `infopdf`";
    $result = $conn->query($select);
    while($row = $result->fetch_object()){
    $pdf = $row->filename;
    $path = $row->directory;
    $date = $row->created_date;
    $file = $path.$pdf;
    // Add header to load pdf file
    'Content-type: application/pdf');
    'Content-Disposition: inline; filename="' .$file. '"');
    'Content-Transfer-Encoding: binary');
    'Accept-Ranges: bytes');

    How Can I Embed Files Into My Sites?

    Filestack ensures a fully responsive user experience through its document viewer. It enables documents to be rendered in the most appealing way to the readers, no matter the resolution or device used. 

    Supporting a wide variety of formats, the document viewer ensures the best way to display your documents, photos, and more in your apps.

    Can I View An Uploaded PDF File In PHP in Document Mode?

    If you’re presenting or handling many user documents at a time, you need to do it in a clear and visually appealing way. 

    Delivery of a file is only successful if it arrives in a user-friendly format, and Filestack’s document viewer achieves this efficiently. During viewing a document, your users can do the following:


    Text search

    Select text

    Zoom in or out

    Rotate pages


    And more!

    Can I Turn My PDF Into A Slideshow?

    With Filestack, any PDF file can beautifully preview into a slideshow. You can do it using the slide processing API task.

    ○ Make your document content the center of attention with slide mode, without overwhelming, haphazard control buttons.

    ○ Merge multiple documents into one PDF and represent them in one slideshow.

    ○ Showcase your files intuitive and easy to navigate using the built-in preview and slider features.

    What Functions Can a Document Viewer Do To View an Uploaded PDF File in PHP?

    Filestack enables preview files in the browser, combining our processing engine with a custom solution built on Mozilla’s PDF.js. 

    Any PDF file or supports converted into a PDF can preview by using the preview processing task in the file URL:

    You can embed this URL in your page as an HTML iframe element. Then Filestack’s JavaScript client provides a convenient preview method to generate and embed these URLs.

    For example:

    <iframe style="width:100%;height:600px;"


    Uploaded PDF File in PHP

    Click here to learn more.

    Which Functions Can a Document Slider Do To View an Uploaded PDF File in PHP?

    Filestack offers preview files in the browser in a slide view. Moreover, any file that is a PDF or supports converted into a PDF can preview by using Filestacks slide processing API task:

    You have to pass in a solitary Filestack handle or an array of handles to merge into one PDF document. This URL can also embed in your page as an HTML iframe element.

    Note: This feature works only with Filestack handles.


    theme (t)  string bright Theme type to use. Available options are: bright or dark.

    engine (e) string poppler Conversion engine to use. Available options are poppler or mupdf.

    Results and performance may vary for each as they use different graphic libraries.


    <iframe style="width:100%;height:600px;"

    No comments:

    Post a Comment