Upload and Save image with PHP and MySql

0
Want create site? Find Free WordPress Themes and plugins.

One of the most sought after script of all time for a PHP beginner is “upload and save image to database “. When I was in my college I always tried to store the image as a whole inside the database, but was unaware of this technique of doing the same. In this tutorial I will explain how to upload image with PHP and save it to MySQL database. Also included in this script is fetching all the images from database and display them in a list.

Scope of this script?

Using this script you can upload images (png, jpg, jpeg, gif,bmp) via php and then store their location/path in MySQL database. Also included in this script is fetching all the uploaded images from mysql database and display them as a list.

Upload Image with Php and save in MySql

Uploading images with PHP and storing it in MySQL database is a four steps process:

  • Setup a form with an image field.
  • Validate the image before uploading.
  • Upload the image with PHP.
  • Store the uploaded image path to the MySQL.

Before you proceed, make sure you have created a database and table in your mysql database server.

Setup a form for uploading image. Don’t forget to add enctype=”multipart/form-data” attribute, which is a common mistake most of beginners does. And always set the method to post.

You can validate the image using javascript or jquery. just add a function that will check for a valid image. Also bind the function on onSubmit event of the form.

Add this JavaScript function inside the head tag of the page.

If in case JavaScript is disabled on user’s computer write the code to validate the image with PHP also. Here’s the script below.

Now the final showdown, uploading the image to the server with PHP and then save the image path to the MySQL database.

Display all uploaded images from MySQL database

To display all the images from the MySQL database as a list. Check this simple script below.

Give your feedback.comments below.

 

Did you find apk for android? You can find new Free Android Games and apps.
Share.

About Author

Entrepreneur & Dreamer I am a passionate Software Professional, love to learn and share my knowledge with others. Software is the hardware of my life.

Leave A Reply

CommentLuv badge

This site uses Akismet to reduce spam. Learn how your comment data is processed.