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

Image Upload With Gallery in PHP Tutorial

0PRO0

Follower Magnet
0 Rep
0
0
0
Rep
0
0 Vouches
0
0
0
Vouches
0
Posts
139
Likes
124
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 300 XP
In this tutorial we will create a Image Upload With Gallery using PHP. This code will automatically arrange the uploaded image into a gallery-style format. The code use MySQLi SELECT parameter to establish a gallery setup page by looping the images in the database server. This is a user-friendly program feel free to modify and use it in your system.

We will be using PHP as a scripting language that interprets in the web server such as XAMPP, WAMP, etc. It is widely used by modern website applications to handle and protect user confidential information.

Getting Started:

First you have to download & install XAMPP or any local server that run PHP scripts. Here's the link for XAMPP server https://www.apachefriends.org/index.html.

Lastly, this is the link for the bootstrap that i used for the layout design https://getbootstrap.com/.

Creating Database

Open your database web server then create a database name in it db_gallery, after that click Import then locate the database file inside the folder of the application then click ok.
2019-04-11_22_43_08-localhost_127.0.0.1_db_gallery_phpmyadmin_4.8.3.png

Or, you can simple copy/paste the SQL

script below to created the table and its column. To do this, navigate your Database to the SQL Tab on the PHPMyAdmin and paste the script in the provided text field and click the Go button.

  1. CREATE

    TABLE

    `image`

    (
  2. `image_

    id`

    int

    (

    11

    )

    NOT

    NULL

    PRIMARY KEY

    AUTO_INCREMENT

    ,
  3. `image`

    varchar

    (

    100

    )

    NOT

    NULL

    ,
  4. `location`

    varchar

    (

    100

    )

    NOT

    NULL


  5. )

    ENGINE

    =

    InnoDB

    DEFAULT

    CHARSET

    =

    latin1;

Creating the database connection

Open your any kind of text editor(notepad++, etc..). Then just copy/paste the code below then name it conn.php.

  1. <?php
  2. $conn

    =

    mysqli_connect

    (

    "localhost"

    ,

    "root"

    ,

    ""

    ,

    "db_gallery"

    )

    ;

  3. if

    (

    !

    $conn

    )

    {
  4. die

    (

    "Error: Failed to connect to database!"

    )

    ;
  5. }
  6. ?>

Creating The Interface

This is where we will create a simple form for our application. To create the forms simply copy and write it into your text editor, then save it as index.php.

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
  5. <link rel="stylesheet" type="text/css" href="css/bootstrap.css"/>
  6. </head>
  7. <body>
  8. <nav class="navbar navbar-default">
  9. <div class="container-fluid">
  10. <a class="navbar-brand" href="https://sourcecodester.com">Sourcecodester</a>
  11. </div>
  12. </nav>
  13. <div class="col-md-3"></div>
  14. <div class="col-md-6 well">
  15. <h3 class="text-primary">PHP - Image Upload With Gallery</h3>
  16. <hr style="border-top:1px dotted #ccc;"/>
  17. <form method="POST" action="upload.php" enctype="multipart/form-data">
  18. <div class="form-inline">
  19. <label>Upload here</label>
  20. <input type="file" name="image" class="form-control" required="required"/>
  21. <button class="btn btn-primary" name="upload"><span class="glyphicon glyphicon-upload"></span> Upload</button>
  22. </div>
  23. </form>
  24. <br />
  25. <div class="alert alert-info">My Gallery</div>
  26. <?php
  27. require

    'conn.php'

    ;

  28. $query

    =

    mysqli_query

    (

    $conn

    ,

    "SELECT * FROM `image`"

    )

    or die

    (

    mysqli_error

    (

    )

    )

    ;
  29. while

    (

    $fetch

    =

    mysqli_fetch_array

    (

    $query

    )

    )

    {
  30. ?>
  31. <div style="border:1px solid #000; height:190px; width:190px; padding:4px; float:left; margin:10px;">
  32. <a href="<?php

    echo

    $fetch

    [

    'location'

    ]

    ?>

    "><img src="<?php

    echo

    $fetch

    [

    'location'

    ]

    ?>

    " width="180" height="180"/></a>
  33. </div>
  34. <?php
  35. }
  36. ?>
  37. </div>
  38. </body>
  39. </html>

Creating PHP Query

This code contains the PHP query of the application. This code will upload and display the images when the button is clicked. To do that just copy and write this block of codes inside the text editor, then save it as upload.php.

  1. <?php
  2. require_once

    'conn.php'

    ;

  3. if

    (

    ISSET

    (

    $_POST

    [

    'upload'

    ]

    )

    )

    {
  4. $image_name

    =

    $_FILES

    [

    'image'

    ]

    [

    'name'

    ]

    ;
  5. $image_temp

    =

    $_FILES

    [

    'image'

    ]

    [

    'tmp_name'

    ]

    ;
  6. $image_size

    =

    $_FILES

    [

    'image'

    ]

    [

    'size'

    ]

    ;
  7. $ext

    =

    explode

    (

    "."

    ,

    $image_name

    )

    ;
  8. $end

    =

    end

    (

    $ext

    )

    ;
  9. $allowed_ext

    =

    array

    (

    "jpg"

    ,

    "jpeg"

    ,

    "gif"

    ,

    "png"

    )

    ;
  10. $name

    =

    time

    (

    )

    .

    "."

    .

    $end

    ;
  11. if

    (

    !

    is_dir

    (

    "upload/"

    )

    )
  12. mkdir

    (

    "upload/"

    )

    ;
  13. $path

    =

    "upload/"

    .

    $name

    ;
  14. if

    (

    in_array

    (

    $end

    ,

    $allowed_ext

    )

    )

    {
  15. if

    (

    $image_size

    >

    5242880

    )

    {
  16. echo

    "<script>alert('File too large!')</script>"

    ;
  17. echo

    "<script>window.location = 'index.php'</script>"

    ;
  18. }

    else

    {
  19. if

    (

    move_uploaded_file

    (

    $image_temp

    ,

    $path

    )

    )

    {
  20. mysqli_query

    (

    $conn

    ,

    "INSERT INTO `image` VALUES('', '$name

    ', '$path

    ')"

    )

    or die

    (

    mysqli_error

    (

    )

    )

    ;
  21. echo

    "<script>alert('Image uploaded!')</script>"

    ;
  22. echo

    "<script>window.location = 'index.php'</script>"

    ;
  23. }
  24. }
  25. }

    else

    {
  26. echo

    "<script>alert('Invalid image format!')</script>"

    ;
  27. echo

    "<script>window.location = 'index.php'</script>"

    ;
  28. }


  29. }
  30. ?>

DEMO Video

There you have it we successfully created Image Upload With Gallery using PHP. I hope that this simple tutorial helps you to what you are looking for. For more updates and tutorials just kindly visit this site.

Enjoy Coding!


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

452,292

323,341

323,350

Top