All TalkersCode Topics

Follow TalkersCode On Social Media - A Social Media Network for developers Join Now ➔

How To Upload Pdf File In MySQL Database Using PHP

Last Updated : Mar 11, 2024

How To Upload Pdf File In MySQL Database Using PHP

In this tutorial we will show you the solution of how to upload pdf file in MySQL database using PHP, in our topic we will learn about how to upload pdf file on mysql database using php.

We can upload pdf files on database then when we need this file we can retrieve and for uploading files it has no limit.

In database we can do store, edit, update or delete these operations and mainly database used for collects and store all user information.

When we need some information from database we can retrieve using php because it is a server side language.

Step by step guide on how to upload pdf file in MySQL database using PHP :-

Here we need to collect server details and make request to server by ‘mysqli_connect()’.

For collect user inputs we need to create html elements of input tags with three types one is ‘text’ for getting image title and ‘file’ type for upload files, another one is for ‘submit’.

When submit button clicks by user it checks uploaded file type is ‘.pdf’ otherwise it gives error on webpage.

If the uploaded file type is ‘.pdf’ then we generating random number before file name for avoid replacing with same name.

Then we need to execute insertion query with ‘uploading file, file title’ details and those details successfully inserted with database. Result will display on plain webpage because we are not styled.

<!DOCTYPE html>
    <title>File Upload</title>
<form method="post" enctype="multipart/form-data">
    <input type="text" name="title">
    <label>File Upload</label>
    <input type="File" name="file">
    <input type="submit" name="submit">
$localhost = "localhost"; #localhost
$dbusername = "root"; #username of phpmyadmin
$dbpassword = ""; #password of phpmyadmin
$dbname = "dbase"; #database name
#connection string
$conn = mysqli_connect($localhost,$dbusername,$dbpassword,$dbname);
if (isset($_POST["submit"]))
if(is_uploaded_file($_FILES["file"]["tmp_name"]) && ($_FILES["file"]["type"] == 'application/pdf') ){
        echo "file is valid";
     #retrieve file title
        $title = $_POST["title"];
    #file name with a random number so that similar dont get replaced
     $pname = rand(1000,10000)."-".$_FILES["file"]["name"];
    #temporary file name to store file
    $tname = $_FILES["file"]["tmp_name"];
     #upload directory path
    $uploads_dir = 'img';
    #TO move the uploaded file to specific location
    move_uploaded_file($tname, $uploads_dir.'/'.$pname);
    #sql query to insert into database
    $sql = "INSERT into fileup(title,image) VALUES('$title','$pname')";
    echo "File Sucessfully uploaded";
        echo "Error";
        echo "file is not valid type";
  1. A php script can be placed anywhere in the document. A php script starts with <?php and end with ?> tags.
  2. The default file extension for php files is “.php” and php each statements end with ‘;’ semicolon. Between start and end tag we need to define our program in php.
  3. First we need to create database with name ‘dbase’ and table name ‘fileup’ with three columns namely ‘id,title,image’. That ‘id’ column type must be ‘primary and auto_increment’ and title type is ‘varchar’, image type is ‘varbinary’.
  4. We need to collect server details so we used variables ‘$localhost,$dbusername,$dbpassword,$dbname’ for collecting our server details.
  5. In variable ‘$localhost’ we stored server name and its server name will available on server page top left end. Most of time it’s name will ‘localhost’.
  6. Variable ‘$dbusername’ used for store ‘user name’ its most of time default value also ‘root’, ‘$dbpassword’ variable for store ‘user password’ and ‘$dbname’ variable for store ‘database name’.
  7. Database name will definitely vary because we can give any name for our database when we are creating. If you use above code then create database on xampp server with name ‘dbase’.
  8. Then using ‘mysqli_connect()’ method with parameter of server details we request server for granting permission.
  9. When request grant means its finding database name on xampp server and when it finds its connection will success. If the database name not exit or any server details not correct then it throws error on browser webpage.
  10. So we have give correct details of server and database then we can achieve result easily. First if() condition for checking whether the submit button clicked or not. Then another if() condition for checking uploaded file type whether ‘.pdf’ or not.
  11. Our uploaded file if ‘.pdf’ means then ‘file type is valid’ message will print on webpage and we storing user input of title of image to variable ‘$title’.
  12. Then we generating random number for appends to file name front because it avoids replacing and we need to give directory name to variable ‘$uploads_dir’. This directory must present in same location of this file location and those file and directory must be in server location.
  13. We created earlier temporary location, its for move our files to that location and we exexcuting insert query for insertion of user inputs ‘title, pdf file’ to our table ‘fileup’ in database ‘dbase’ on server.
  14. Next if() condition for checks our insertion whether executed successfully or not. When it success it throws ‘file successfully uploaded’ message on webpage otherwise error message will thrown on webpage.
  15. Then result will printed on our webpage using ‘echo()’ with ‘connection succeed’ string. It also a statement for print any values in php.

Conclusion :-

In conclusion we are able to know how to upload pdf file on mysql database using php.

When work with php we need to create and process php files at server location and then we need to start the server before execute the program.

When user gives inputs and clicks submit button it will loads whole code, if user uploaded file type is ‘.pdf’ it will successfully uploaded on our database on server with success message display on webpage for user verification, otherwise it throws error message on webpage of browser.

I hope this tutorial on how to upload pdf file in MySQL database using PHP helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Pragati

Experienced coding content writer who enjoys breaking down complex concepts in programming languages like Java, Python, C, and C++. Over three years of experience producing interesting and relevant content for a variety of entities. Committed to providing concise and easy-to-understand articles that assist readers in easily understanding technology and industry trends in the world of coding and software development.

Follow Pragati On Linkedin 🡪