How To Create Image Upload In PHP | CampCodes ...

How to Create Image Upload in PHP

May 24, 2020
image uploader in php

This instructional tutorial will tell you the best way to make a straightforward picture uploader utilizing PHP/MySQLi. This instructional exercise does exclude a decent structure yet will give you a thought on the best way to transfer picture utilizing PHP/MySQLi.

Making our Database

To begin with, we will make a database that will store the area of our picture.

1. Open PHPMyAdmin.

2. Click the database, make a database and name it as image_upload.

3. In the wake of making a database, click the SQL and glue the underneath code. See picture underneath for point by point guidance.

`imageid` int(11)  NOT NULL AUTO_INCREMENT,
  `img_location` varchar(150) NOT NULL,
PRIMARY KEY (`imageid`)

image uploader

Making our Connection

Next, we make a database association and spare it as “conn.php”. This record will fill in as our scaffold between our structure and our database.

$con = mysqli_connect("localhost","root","","image_upload");
// Check connection
if (mysqli_connect_errno())
  echo "Failed to connect to MySQL: " . mysqli_connect_error();

Making our Output Folder

Subsequent stage is to make an envelope that will store our transferred pictures and name it as “upload”.

Making our Form

Following stage is to make our structure and spare it as “index.php”. This is likewise where we can see our transferred pictures. To make the structure, open your HTML code supervisor and glue the code underneath after the tag.

<html lang="en">
<meta charset="UTF-8">
<title>Easy and Simple Image Upload</title>
    Uploaded Images:
        $query=mysqli_query($con,"select * from image_tb");
                <img src="<?php echo $row['img_location']; ?>">
    <form method="POST" action="upload.php" enctype="multipart/form-data">
    <label>Image:</label><input type="file" name="image">
    <button type="submit">Upload</button>

Composing our Upload Script

In conclusion, we make a content that will spare our transferred pictures and name it as “upload.php”.

	$newFilename=$fileinfo['filename'] ."_". time() . "." . $fileinfo['extension'];
	move_uploaded_file($_FILES["image"]["tmp_name"],"upload/" . $newFilename);
	$location="upload/" . $newFilename;
	mysqli_query($con,"insert into image_tb (img_location) values ('$location')");

download button

This is a free education portal. You can use every source code in your project without asking permission to the author. Share our website to everyone to make our community of programmers grow more.

Leave a Reply

Your email address will not be published. Required fields are marked *