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

How to Create Image Upload in PHP

May 24, 2020
PHP
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.

CREATE TABLE IF NOT EXISTS `image_tb` (
`imageid` int(11)  NOT NULL AUTO_INCREMENT,
  `img_location` varchar(150) NOT NULL,
PRIMARY KEY (`imageid`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

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.

<?php
$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">
<head>
<meta charset="UTF-8">
<title>Easy and Simple Image Upload</title>
</head>
<body>
    <div>
    Uploaded Images:
    <?php
        include('conn.php');
        $query=mysqli_query($con,"select * from image_tb");
        while($row=mysqli_fetch_array($query)){
            ?>
                <img src="<?php echo $row['img_location']; ?>">
            <?php
        }
    ?>
    </div>
    <div>
    <form method="POST" action="upload.php" enctype="multipart/form-data">
    <label>Image:</label><input type="file" name="image">
    <button type="submit">Upload</button>
    </form>
    </div>
</body>
</html>

Composing our Upload Script

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

<?php
	include('conn.php');
	$fileinfo=PATHINFO($_FILES["image"]["name"]);
	$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')");
	header('location:index.php');
?>

download button

https://www.campcodes.com/

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 *