• We just launched and are currently in beta. Join us as we build and grow the community.

Easy and Simple way to Upload Image using PHP/MySQLi

mawwximus94

Shoujo Dreamer
M Rep
0
0
0
Rep
0
M Vouches
0
0
0
Vouches
0
Posts
48
Likes
99
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 300 XP
This tutorial will show you how to create a simple image uploader using PHP/MySQLi. This tutorial does not include a good design but will give you an idea of how to upload images using PHP/MySQLi. Our goal in this tutorial is to create an Image Upload function into our simple Web App and Display the images after saving them into the database. The input that we will be using is an input file that only accepts any types of images.

Getting Starte

Please download and install the ff software if not yet installed in your desktop or laptop.


Creating our Database

First, we're going to create a database that will store the location of our image.

  1. Open PHPMyAdmin.
  2. Click databases, create a database and name it as image_upload.
  3. After creating a database, click the SQL and paste the below code. See image below for detailed instruction.

  1. CREATE

    TABLE

    [url=http://dev.mysql.com/doc/refman/%35%2E%31/en/control-flow-functions.html]IF

    NOT

    EXISTS

    [/url] `image_

    tb`

    (
  2. `imageid`

    int

    (

    11

    )

    NOT

    NULL

    AUTO_INCREMENT

    ,
  3. `img_

    location`

    varchar

    (

    150

    )

    NOT

    NULL

    ,
  4. PRIMARY KEY

    (

    `imageid`

    )
  5. )

    ENGINE

    =

    InnoDB

    DEFAULT

    CHARSET

    =

    latin1;

steps_img.png

Creating our Connection

Next, we create a database connection and save it as conn.php

. This file will serve as our bridge between our form and our database.

  1. <?php
  2. $con

    =

    mysqli_connect

    (

    "localhost"

    ,

    "root"

    ,

    ""

    ,

    "image_upload"

    )

    ;

  3. // Check connection
  4. if

    (

    mysqli_connect_errno

    (

    )

    )
  5. {
  6. echo

    "Failed to connect to MySQL: "

    .

    mysqli_connect_error

    (

    )

    ;
  7. }
  8. ?>

Creating our Output Folder

Next step is to create a folder that will store our uploaded images and name it as upload

.

Creating our Form

Next step is to create our form and save it as index.php

. This is also the place where we can see our uploaded images. To create the form, open your HTML code editor and paste the code below after the tag.

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Easy and Simple Image Upload</title>
  6. <style>
  7. .container{
  8. display: flex;
  9. width:calc(100%);
  10. flex-wrap: wrap;
  11. }
  12. .container img{
  13. width: calc(18%);
  14. height: 15vw;
  15. object-fit: contain;
  16. background: gray;
  17. border: 1px solid black;
  18. margin: 5px;
  19. }
  20. </style>
  21. </head>
  22. <body>

  23. <h2><strong>Uploaded Images:</strong></h2>
  24. <div class="container">
  25. <br>
  26. <?php
  27. include

    (

    'conn.php'

    )

    ;
  28. $query

    =

    mysqli_query

    (

    $con

    ,

    "select * from image_tb"

    )

    ;
  29. if

    (

    mysqli_num_rows

    (

    $query

    )

    >

    0

    )

    {
  30. while

    (

    $row

    =

    mysqli_fetch_array

    (

    $query

    )

    )

    {
  31. ?>
  32. <img src="<?php

    echo

    $row

    [

    'img_location'

    ]

    ;

    ?>

    ">
  33. <?php
  34. }
  35. }

    else

    {
  36. echo

    "<p><strong>No Images uploaded yet.</strong></p>"

    ;
  37. }
  38. ?>
  39. </div>
  40. <div>
  41. <form method="POST" action="upload.php" enctype="multipart/form-data">
  42. <label>Image:</label><input type="file" name="image" accept="image/*">
  43. <button type="submit">Upload</button>
  44. </form>
  45. </div>
  46. </body>
  47. </html>

Writing our Upload Script

Lastly, we create a script that will save our uploaded images and name it as upload.php

.

  1. <?php
  2. include

    (

    'conn.php'

    )

    ;
  3. if

    (

    !

    empty

    (

    $_FILES

    [

    "image"

    ]

    [

    "tmp_name"

    ]

    )

    )

    {
  4. $fileinfo

    =

    PATHINFO

    (

    $_FILES

    [

    "image"

    ]

    [

    "name"

    ]

    )

    ;
  5. $newFilename

    =

    $fileinfo

    [

    'filename'

    ]

    .

    "_"

    .

    time

    (

    )

    .

    "."

    .

    $fileinfo

    [

    'extension'

    ]

    ;
  6. move_uploaded_file

    (

    $_FILES

    [

    "image"

    ]

    [

    "tmp_name"

    ]

    ,

    "upload/"

    .

    $newFilename

    )

    ;
  7. $location

    =

    "upload/"

    .

    $newFilename

    ;

  8. mysqli_query

    (

    $con

    ,

    "insert into image_tb (img_location) values ('$location

    ')"

    )

    ;
  9. header

    (

    'location:index.php'

    )

    ;
  10. }

    else

    {
  11. echo

    "<script>alert('No Image selected.');location.replace('./index.php');</script>"

    ;
  12. }

  13. ?>

DEMO

That's it! Now test your work if it is working as it is. If not, try to repeat the tutorial from the start or you may download the working source code that I uploaded with this tutorial. Feel free to download, test, and differentiate it with your work so you will know which part you have missed.

That's the end of this tutorial. I hope this tutorial helps you with what you are looking for and I hope this helps you to learn something useful for your future projects.

Happy Coding :)


Download
You must upgrade your account or reply in the thread to view the hidden content.
 

452,496

329,696

329,704

Top