Upload and Store Image File in Database using PHP and MySQL3 min read

In this tutorial, we create a form that takes an image and some text. When the user selects an image and enters some text and clicks the submit button, the data is submitted to the server. PHP now grabs the image and saves it in a folder in the project, and then saves the text in the database together with a link pointing to the image in the folder.

Server-side file upload can be easily implemented using PHP. There are various ways available to upload image to server and display images on the webpage. Generally, in a dynamic web application, the uploaded image is stored in a directory of the server and the file name is inserted in the database. Later, the images are retrieved from the server based on the file name stored in the database and display on the web page.

The image can be uploaded directly to the database without storing on the server. But it will increase the database size and web page load time. So, it’s always a good idea to upload images to the server and store file names in the database. In this tutorial, we will show you the entire process to upload and store the image file in MySQL database using PHP.

Create a database called image_upload and create a table called images with fields:

  • id – int(11)
  • image – varchar(100)
  • image_text – text

Create a file called index.php and page the following code in it (the complete code):


And that’s all. 

Be sure to include the enctype in your form tag. Like this:

Without the attribute enctype=”multipart/form-data”, the image won’t be uploaded. enctype is the encoding type that specifies how the form-data should be encoded when submitting the form. Without it file uploads won’t work.

Here we have shown the most effective way to implement image upload functionality on the website. You can easily extend the file upload functionality as per your requirements.

Leave a Comment