How to Upload a Image to a Server Using an Html Input

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

Concluding Updated : Apr fifteen, 2022

IN - HTML PHP MySQL

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

All y'all need to have knowledge of HTML, PHP and MySQL. Y'all may as well like ajax epitome upload to upload images without refreshing the webpage.

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

To Upload The Paradigm In Database information technology takes simply 3 steps:-

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

Stride i. Make a HTML form

We brand a HTML form with post method and save information technology with a name upload.html

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

We submit the data from this HTML form to getdata.php where the epitome is going to shop in the database.

You may as well like preview prototype before upload.

Step two. Connect To Database and Store Prototype

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

You can connect to any database from which you lot desire to become the data. In this we utilize sample database named " demo ".

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

To store the image into database you have to utilise blob datatype of your paradigm column in your table. MySQL uses Blob to store binary information and images is besides a binary information.

You tin can utilise any kind of BLOB TINYBLOB, BLOB, MEDIUMBLOB, LONGBLOB as per the size of your image.You may also similar upload image from URL using PHP.

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

Footstep 3. Displaying the stored Images from database

This is fetch_image.php file

            <?php            header("content-type:prototype/jpeg");  $host = 'localhost'; $user = 'root'; $pass = ' ';  mysql_connect($host, $user, $pass);  mysql_select_db('demo');  $name=$_GET['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"]; } repeat $image;            ?>          

Now we desire to brandish the epitome we make another file display_image.php.

            <html>            <body>            <class method="GET" action=" " >            <input type="file" name="your_imagename">            <input type="submit" name="display_image" value="Display">            </form>            </body>            </html>            <?php            $getname = $_GET[' your_imagename '];  echo "< img src = fetch_image.php?proper noun=".$getname." width=200 height=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 prototype to your server or directory
  3. Displaying the Image

Footstep 1. Brand a HTML form

You can apply same HTML class equally we made higher up to upload the paradigm

Footstep 2. Storing epitome to the Server

This is store_image.php file

            <?php            $host = 'localhost'; $user = 'root'; $laissez passer = ' ';  mysql_connect($host, $user, $laissez passer);  mysql_select_db('demo');  $upload_image=$_FILES[" myimage "][ "name" ];  $folder="/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);            ?>          

Step 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, $laissez passer);  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"];  repeat "img src=".$image_path."/".$image_name." width=100 height=100"; }            ?>          

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

You tin customize this lawmaking further every bit per your requirement. And please feel free to give comments on this tutorial.

martinnoway1968.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 a Image to a Server Using an Html Input"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel