How to Upload PDF Files in PHP

Written by tiesha whatley
  • Share
  • Tweet
  • Share
  • Pin
  • Email
How to Upload PDF Files in PHP
(Comstock Images/Comstock/Getty Images)

PHP has an upload function that handles uploading files to the web server. Upload PDF files to display on your website to offer your visitors information in a universal file format. To upload PDF files specifically, you will need to make allowances in the PHP coding that only PDF files are allowed. This comes in handy when allowing others to upload to your server while keeping control of what they upload. Create a web form that works as a user interface for uploading PDF files to your server.

Skill level:


  1. 1

    Create a new HTML document in a text-editing program, such as Notepad, or a web-authoring application, such as Dreamweaver. Switch to "code" or "HTML" view if using a web-authoring program.

  2. 2

    Type in the HTML code to create the form that your users will use as the interface to upload the PDF documents.




    <form action="<?php print $PHP_SELF?>" enctype="multipart/form-data" method="post">

    Choose a PDF document to upload: <input name="uploadedfile" type="file" />

    <p><input type="submit" name="submit" value="Submit" /></p>


    This will create a simple form that has a field to specify which document to upload and a submit button that will use the PHP code to handle uploading the PDF form. Omit the quotation marks at the start and end of the code.

  3. 3

    Press "Enter" a couple of times to skip a couple of lines. This keeps the code formatted and easier to read in the event you need to go back and change something down the line.

  4. 4

    Indicate in the code that PHP programming is about to start. Type in "<?php" to indicate this. Skip a line and type in the rest of the code that will handle uploading the PDF document once the user has clicked on the "Submit" button on the form you created.

    "define ("FILEREPOSITORY","./");

    if (is_uploaded_file($_FILES['pdf_document']['tmp_name'])) {

      if ($_FILES['pdf_document']['type'] != "application/pdf") {
         echo "&lt;p&gt;Files must be uploaded in PDF format.&lt;/p&gt;";
      } else {
         $name = $_POST['name'];
         $result = move_uploaded_file($_FILES['pdf_document']['tmp_name'], FILEREPOSITORY."/$name.pdf");
         if ($result == 1) echo "&lt;p&gt;PDF document successfully uploaded.&lt;/p&gt;";
         else echo "&lt;p&gt;There was a problem uploading the file. Check that the file is valid and try again.&lt;/p&gt;";
      } #endIF

    } #endIF


    Take out the quotation marks from the first and last lines in the code. The code renames the file while it is saving it to the server. This is important in the event that two people are trying to load separate PDF documents with the same file names. Close up the HTML code with:




  5. 5

    Save the document with the .php file extension. Upload the document to your web server.

Don't Miss

  • All types
  • Articles
  • Slideshows
  • Videos
  • Most relevant
  • Most popular
  • Most recent

No articles available

No slideshows available

No videos available

By using the site, you consent to the use of cookies. For more information, please see our Cookie policy.