Skip to main content

Simple Image Upload Using AngularJS/PHP

Screenshot
Body

In this tutorial we will create a Simple Image Upload Using AngularJs/PHP. AngularJS is a structural framework for dynamic web apps. It is a kind of template that extends HTML to a new level of coding techniques. By using angularjs will try also to save the image through PHP server. So Let start coding!!

Creating Database

To create database open your local server(wamp, xamp, etc...). Then create a database and name it "db_image". After that click SQL then copy/paste the code below

  1. <br />
  2. CREATE TABLE `photo` (<br />
  3. `photo_id` int(11) NOT NULL,<br />
  4. `photo_name` varchar(255) NOT NULL,<br />
  5. PRIMARY KEY (`photo_id`)<br />

Creating The Mark Up Form

This where the file type will be shown. To do that copy/paste the code below then name it "index.php"

  1. <br />
  2. <!DOCTYPE html><br />
  3. <html><br />
  4. <head><br />
  5. <meta charset = "UTF-8" name = "viewport" content = "width=device-width, initial-scale=1"/><br />
  6. <title>Image Upload Using AngularJs</title><br />
  7. <link rel = "stylesheet" type = "text/css" href = "css/bootstrap.css"/><br />
  8. <script src="js/angular.js"></script><br />
  9. </head><br />
  10. <body ng-app="myModule" ng-controller="myController"><br />
  11. <nav class = "navbar navbar-default"><br />
  12. <div class = "container-fluid"><br />
  13. <a class = "navbar-brand" href = "https://sourcecodester.com">Sourcecodester</a><br />
  14. </div><br />
  15. </nav><br />
  16. <div class = "row"><br />
  17. <div class = "col-md-3"></div><br />
  18. <div class = "col-md-6 well"><br />
  19. <h3 class = "text-primary">Simple Image Upload Using AngularJs</h3><br />
  20. <hr style = "boreder-top:1px dotted #000;"/><br />
  21. <form ng-submit="submit()" name="form" role="form"><br />
  22. <div class = "form-inline"><br />
  23. <input ng-model="form.image" class = "form-control" type="file" accept="image/*" onchange="angular.element(this).scope().uploadedFile(this)" style="width:400px" ><br />
  24. <button class = "btn btn-primary"><span class = "glyphicon glyphicon-upload"></span> Upload</button><br />
  25. <a href = "result.php" >See All Upload Images</a><br />
  26. </div><br />
  27. <br/><br />
  28. <center><img ng-src="{{image_source}}" style="width:600px;"></center><br />
  29. </form><br />
  30. </div><br />
  31. </div><br />
  32. </body><br />
  33. <script src = "js/app.js"></script><br />
  34. </html><br />

Creating AngularJs Directives
This where the angularjs directive will be coded. Copy/paste the code below and name it "app.js"

  1. <br />
  2. var app = angular.module('myModule',[]);</p>
  3. <p> app.controller('myController', function($scope, $http) {</p>
  4. <p> $scope.form = [];<br />
  5. $scope.files = [];</p>
  6. <p> $scope.submit = function() {<br />
  7. $scope.form.image = $scope.files[0];</p>
  8. <p> $http({<br />
  9. method : 'POST',<br />
  10. url : 'upload.php',<br />
  11. processData: false,<br />
  12. transformRequest: function (data) {<br />
  13. var formData = new FormData();<br />
  14. formData.append("image", $scope.form.image);<br />
  15. return formData;<br />
  16. },<br />
  17. data : $scope.form,<br />
  18. headers: {<br />
  19. 'Content-Type': undefined<br />
  20. }<br />
  21. }).then(function(data){<br />
  22. alert("Successfully Upload An Image!");<br />
  23. window.location = "index.php";<br />
  24. });</p>
  25. <p> };</p>
  26. <p> $scope.uploadedFile = function(element) {<br />
  27. $scope.currentFile = element.files[0];<br />
  28. var reader = new FileReader();<br />
  29. reader.onload = function(event) {<br />
  30. $scope.image_source = event.target.result<br />
  31. $scope.$apply(function($scope) {<br />
  32. $scope.files = element.files;<br />
  33. });<br />
  34. }<br />
  35. reader.readAsDataURL(element.files[0]);<br />
  36. }<br />
  37. });<br />

The code above processed the image data. The $scope.uploadedFile will display the image after the user selected the image to be upload.The $scope.submit function will submit the stored image selected by a user to the database server.

Creating The Upload Query
This where the file will be stored after getting the value from the input file type. To create this copy/paste the code then name it "upload.php"

  1. <br />
  2. <?php<br />
  3. if(!empty($_FILES['image'])){<br />
  4. $conn = new mysqli("localhost", "root", "", "db_image") or die(mysqli_error());<br />
  5. $path = pathinfo($_FILES['image']['name'],PATHINFO_EXTENSION);<br />
  6. $image = time().'.'.$path;<br />
  7. move_uploaded_file($_FILES["image"]["tmp_name"], 'images/'.$image);<br />
  8. $conn->query("INSERT INTO `photo` (photo_name) VALUES('$image')") or die(mysqli_error());<br />
  9. }else{<br />
  10. echo "<script>Error!</script>";<br />
  11. }<br />
  12. ?><br />

The code above will now sending the image to the database server after storing the value within the input file type.

Displaying All The Stored Images
This is where images will be displayed. Copy/paste the code below and name it "result.php"

  1. <br />
  2. <!DOCTYPE html><br />
  3. <html><br />
  4. <head><br />
  5. <meta charset = "UTF-8" name = "viewport" content = "width=device-width, initial-scale=1"/><br />
  6. <link rel = "stylesheet" type = "text/css" href = "css/bootstrap.css" /><br />
  7. </head><br />
  8. <body><br />
  9. <nav class = "navbar navbar-default"><br />
  10. <div class = "container-fluid"><br />
  11. <a class = "navbar-brand" href = "https://sourcecodester.com">Sourcecodester</a><br />
  12. </div><br />
  13. </nav><br />
  14. <div class = "row"><br />
  15. <div class = "col-md-3"></div><br />
  16. <div class = "col-md-6 well"><br />
  17. <h3 class = "text-primary">Simple Image Upload Using AngularJs</h3><br />
  18. <hr style = "boreder-top:1px dotted #000;"/><br />
  19. <a class = "btn btn-success" href = "index.php"><span class = "glyphicon glyphicon-hand-right"></span> Back</a><br />
  20. <table class = "table table-hover"><br />
  21. <thead><br />
  22. <tr><br />
  23. <th>Name<th><br />
  24. <th>Image<th><br />
  25. </tr><br />
  26. </thead><br />
  27. <tbody><br />
  28. <?php<br />
  29. $conn = new mysqli("localhost", "root", "", "db_image") or die(mysqli_error());<br />
  30. $query = $conn->query("SELECT * FROM `photo`") or die(mysqli_error());<br />
  31. while($fetch = $query->fetch_array()){<br />
  32. ?><br />
  33. <tr><br />
  34. <td><?php echo $fetch['photo_name']?></td><br />
  35. <td colspan = "2"><center><?php echo '<img src = "images/'.$fetch['photo_name'].'" width = "150px" height = "70px"/>'?></center></td><br />
  36. </tr><br />
  37. <?php<br />
  38. }<br />
  39. ?><br />
  40. </tbody><br />
  41. </table><br />
  42. </div><br />
  43. </div><br />
  44. </body><br />
  45. </html><br />

There you have we created a simple image upload using AngularJs/PHP. I hope that this tutorial help you to your project. For more updates and tutorials just visit this site, and don't forget to LIKE & SHARE. Enjoy Coding!!

Add new comment