How to Upload Images on Database and Display Them on Page Php

Upload Image to Database and Server using HTML,PHP and MySQL

Last Updated : Apr 15, 2022

IN - HTML PHP MySQL

Paradigm Uploading is very easy there are two ways y'all tin can upload the paradigm either to the database or in the server as you like. In this tutorial we use both ways to upload and display the image.

All yous demand to accept knowledge of HTML, PHP and MySQL. You lot may also like ajax paradigm upload to upload images without refreshing the webpage.

Upload Image to Database and Server using HTML,PHP and MySQL

To Upload The Image In Database it takes but three steps:-

  1. Brand a HTML form to upload the image
  2. Connect to the database and store image
  3. Displaying the Image

Step 1. Make a HTML grade

We make a HTML grade with post method and save it with a proper noun upload.html

            <html>            <body>            <grade method="POST" action="getdata.php" enctype="multipart/form-data">            <input blazon="file" name="myimage">            <input type="submit" name="submit_image" value="Upload">            </grade>            </trunk>            </html>          

Nosotros submit the data from this HTML form to getdata.php where the paradigm is going to store in the database.

You may too similar preview epitome before upload.

Step 2. Connect To Database and Store Image

In this step we have to connect to the database to shop the image in database.

You tin can connect to any database from which you lot want to get the data. In this we use sample database named " demo ".

            <?php            $host = 'localhost'; $user = 'root'; $laissez passer = ' ';  mysql_connect($host, $user, $pass);  mysql_select_db('demo');            ?>          

To store the epitome into database you take to utilize hulk datatype of your paradigm column in your tabular array. MySQL uses Blob to store binary information and images is likewise a binary data.

Y'all can utilize any kind of BLOB TINYBLOB, BLOB, MEDIUMBLOB, LONGBLOB as per the size of your image.You may also like upload image from URL using PHP.

            <?php            $imagename=$_FILES["myimage"]["proper name"];   //Get the content of the image and and then add slashes to it  $imagetmp=addslashes (file_get_contents($_FILES['myimage']['tmp_name']));  //Insert the image name and image content in image_table $insert_image="INSERT INTO image_table VALUES('$imagetmp','$imagename')";  mysql_query($insert_image);            ?>          

Step three. Displaying the stored Images from database

This is fetch_image.php file

            <?php            header("content-blazon:image/jpeg");  $host = 'localhost'; $user = 'root'; $laissez passer = ' ';  mysql_connect($host, $user, $pass);  mysql_select_db('demo');  $name=$_GET['proper name'];  $select_image="select * from image_table where imagename='$name'";  $var=mysql_query($select_image);  if($row=mysql_fetch_array($var)) {  $image_name=$row["imagename"];  $image_content=$row["imagecontent"]; } echo $paradigm;            ?>          

Now we want to display the epitome we make some other file display_image.php.

            <html>            <body>            <grade method="GET" activeness=" " >            <input blazon="file" proper name="your_imagename">            <input type="submit" name="display_image" value="Display">            </form>            </body>            </html>            <?php            $getname = $_GET[' your_imagename '];  repeat "< img src = fetch_image.php?name=".$getname." width=200 superlative=200 >";            ?>          

To Upload The Image In Server it takes only three steps:-

  1. Make a HTML form to upload the image
  2. Store image path to database and store the image to your server or directory
  3. Displaying the Epitome

Step 1. Make a HTML form

Yous can use same HTML class equally we fabricated above to upload the image

Step 2. Storing image to the Server

This is store_image.php file

            <?php            $host = 'localhost'; $user = 'root'; $laissez passer = ' ';  mysql_connect($host, $user, $pass);  mysql_select_db('demo');  $upload_image=$_FILES[" myimage "][ "name" ];  $binder="/xampp/htdocs/images/";  move_uploaded_file($_FILES[" myimage "][" tmp_name "], "$folder".$_FILES[" myimage "][" name "]);  $insert_path="INSERT INTO image_table VALUES('$folder','$upload_image')";  $var=mysql_query($inser_path);            ?>          

Footstep 3. Displaying the Images

To display images you have to get the file name and file path from the database.

This is fetch_image.php file

            <?php            $host = 'localhost'; $user = 'root'; $laissez passer = ' ';  mysql_connect($host, $user, $pass);  mysql_select_db('demo');  $select_path="select * from image_table";  $var=mysql_query($select_path);  while($row=mysql_fetch_array($var)) {  $image_name=$row["imagename"];  $image_path=$row["imagepath"];  echo "img src=".$image_path."/".$image_name." width=100 height=100"; }            ?>          

That's all, this is how to upload image to database or in a directory with the help of HTML, PHP and MySQL.

You can customize this code further as per your requirement. And delight feel free to give comments on this tutorial.

tibbslaustim.blogspot.com

Source: http://talkerscode.com/webtricks/upload%20image%20to%20database%20and%20server%20using%20HTML,PHP%20and%20MySQL.php

0 Response to "How to Upload Images on Database and Display Them on Page Php"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel