How To Create File Image Upload With Gallery Using PHP

Related Code: Upload Multiple Images In PHP Related Code: File Upload With Progress Bar Using PHP In this article, we are going to learn on How To Create File Image Upload With Gallery Using PHP. This tutorial creates using PHP/MySQL. You can use this source code to your website or social networking site project to have a photo gallery or you can include this to your User Registration With Photo, etc. Let's start with:

Creating our Table

For the first step, we are going to make our database. To create a database:
  1. Open PHPMyAdmin
  2. Create a database and name it as "biobook".
  3. After creating a database name, click the SQL and kindly copy the code below.
  1. --
  2. -- Table structure for table `photos`
  3. --
  4.  
  5. CREATE TABLE IF NOT EXISTS `photos` (
  6. `photo_id` int(100) NOT NULL AUTO_INCREMENT,
  7. `location` varchar(100) NOT NULL,
  8. `user_id` varchar(100) NOT NULL,
  9. `date_added` varchar(100) NOT NULL,
  10. PRIMARY KEY (`photo_id`)

Form Field

This form where the user can choose their photo to upload then it will show in the gallery.
  1. <div id="right-nav">
  2. <h1>Your Photos</h1>
  3. <div>
  4. <form method="post" action="add_photo.php" enctype="multipart/form-data">
  5. <input type="file" name="image">
  6. <button class="btn-submit-photo" name="Submit" value="Log out">Add Photos</button>
  7. </form>
  8. <hr />
  9. </div>
  10. <?php
  11. include("includes/database.php");
  12. $query=mySQL_query("SELECT * from photos where user_id='$id' ") or die(mySQL_error());
  13. while($row=mySQL_fetch_array($query)){
  14. $id = $row['photo_id'];
  15. ?>
  16. <div class="photo-select">
  17. <center>
  18. <img src="<?php echo $row['location']; ?>">
  19. <hr>
  20. <a href="delete_photos.php<?php echo '?id='.$id; ?>" class="btn-delete-photos">Delete</a>
  21. </center>
  22. </div>
  23. <?php
  24. }
  25. ?>
  26. </div>

Creating Connection

This PHP source code is our database connection, kindly copy and save it as "database.php".
  1. <?php
  2. mysql_select_db('biobook',mysql_connect('localhost','root',''))or die(mysql_error());
  3. ?>

File Upload PHP Script

This source code will execute to save the photos to database and save it as "add_photo.php".
  1. <?php
  2. include('includes/database.php');
  3. include('session.php');
  4.  
  5. if (!isset($_FILES['image']['tmp_name'])) {
  6. echo "";
  7. }else{
  8. $file=$_FILES['image']['tmp_name'];
  9. $image = $_FILES["image"] ["name"];
  10. $image_name= addslashes($_FILES['image']['name']);
  11. $size = $_FILES["image"] ["size"];
  12. $error = $_FILES["image"] ["error"];
  13.  
  14. if ($error > 0){
  15. die("Error uploading file! Code $error.");
  16. }else{
  17. if($size > 10000000) //conditions for the file
  18. {
  19. die("Format is not allowed or file size is too big!");
  20. }
  21.  
  22. else
  23. {
  24.  
  25. move_uploaded_file($_FILES["image"]["tmp_name"],"upload/" . $_FILES["image"]["name"]);
  26. $location="upload/" . $_FILES["image"]["name"];
  27. $user=$_SESSION['id'];
  28.  
  29. $update=mysql_query(" INSERT INTO photos (location,user_id,date_added)
  30. VALUES ('$location','$id',NOW()) ") or die (mySQL_error());
  31.  
  32. }
  33. header('location:photos.php');
  34.  
  35.  
  36. }
  37. }
  38. ?>
And, that's it... You've been successfully created your File Image Upload With Gallery. This is the Output: ResultRelated Code: Upload Multiple Images In PHP Related Code: File Upload With Progress Bar Using PHP Just download the complete source code below. By clicking the "Download Code" button. Share us your thoughts and comments below. Thank you so much for dropping by and reading this tutorial post. For more updates, don’t hesitate and feel free to visit this website more often and please share this with your friends or email me at [email protected]. Practice Coding. Thank you very much.

Add new comment