Simple Image Upload Using AngularJS/PHP

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. CREATE TABLE `photo` (
  2. `photo_id` int(11) NOT NULL,
  3. `photo_name` varchar(255) NOT NULL,
  4. PRIMARY KEY (`photo_id`)
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. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset = "UTF-8" name = "viewport" content = "width=device-width, initial-scale=1"/>
  5. <title>Image Upload Using AngularJs</title>
  6. <link rel = "stylesheet" type = "text/css" href = "css/bootstrap.css"/>
  7. <script src="js/angular.js"></script>
  8. </head>
  9. <body ng-app="myModule" ng-controller="myController">
  10. <nav class = "navbar navbar-default">
  11. <div class = "container-fluid">
  12. <a class = "navbar-brand" href = "https://sourcecodester.com">Sourcecodester</a>
  13. </div>
  14. </nav>
  15. <div class = "row">
  16. <div class = "col-md-3"></div>
  17. <div class = "col-md-6 well">
  18. <h3 class = "text-primary">Simple Image Upload Using AngularJs</h3>
  19. <hr style = "boreder-top:1px dotted #000;"/>
  20. <form ng-submit="submit()" name="form" role="form">
  21. <div class = "form-inline">
  22. <input ng-model="form.image" class = "form-control" type="file" accept="image/*" onchange="angular.element(this).scope().uploadedFile(this)" style="width:400px" >
  23. <button class = "btn btn-primary"><span class = "glyphicon glyphicon-upload"></span> Upload</button>
  24. <a href = "result.php" >See All Upload Images</a>
  25. </div>
  26. <br/>
  27. <center><img ng-src="{{image_source}}" style="width:600px;"></center>
  28. </form>
  29. </div>
  30. </div>
  31. </body>
  32. <script src = "js/app.js"></script>
  33. </html>
Creating AngularJs Directives This where the angularjs directive will be coded. Copy/paste the code below and name it "app.js"
  1. var app = angular.module('myModule',[]);
  2.  
  3. app.controller('myController', function($scope, $http) {
  4.  
  5. $scope.form = [];
  6. $scope.files = [];
  7.  
  8. $scope.submit = function() {
  9. $scope.form.image = $scope.files[0];
  10.  
  11. $http({
  12. method : 'POST',
  13. url : 'upload.php',
  14. processData: false,
  15. transformRequest: function (data) {
  16. var formData = new FormData();
  17. formData.append("image", $scope.form.image);
  18. return formData;
  19. },
  20. data : $scope.form,
  21. headers: {
  22. 'Content-Type': undefined
  23. }
  24. }).then(function(data){
  25. alert("Successfully Upload An Image!");
  26. window.location = "index.php";
  27. });
  28.  
  29. };
  30.  
  31. $scope.uploadedFile = function(element) {
  32. $scope.currentFile = element.files[0];
  33. var reader = new FileReader();
  34. reader.onload = function(event) {
  35. $scope.image_source = event.target.result
  36. $scope.$apply(function($scope) {
  37. $scope.files = element.files;
  38. });
  39. }
  40. reader.readAsDataURL(element.files[0]);
  41. }
  42. });
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. <?php
  2. if(!empty($_FILES['image'])){
  3. $conn = new mysqli("localhost", "root", "", "db_image") or die(mysqli_error());
  4. $path = pathinfo($_FILES['image']['name'],PATHINFO_EXTENSION);
  5. $image = time().'.'.$path;
  6. move_uploaded_file($_FILES["image"]["tmp_name"], 'images/'.$image);
  7. $conn->query("INSERT INTO `photo` (photo_name) VALUES('$image')") or die(mysqli_error());
  8. }else{
  9. echo "<script>Error!</script>";
  10. }
  11. ?>
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. <!DOCTYPE html>
  2. <html>
  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 = "row">
  14. <div class = "col-md-3"></div>
  15. <div class = "col-md-6 well">
  16. <h3 class = "text-primary">Simple Image Upload Using AngularJs</h3>
  17. <hr style = "boreder-top:1px dotted #000;"/>
  18. <a class = "btn btn-success" href = "index.php"><span class = "glyphicon glyphicon-hand-right"></span> Back</a>
  19. <table class = "table table-hover">
  20. <thead>
  21. <tr>
  22. <th>Name<th>
  23. <th>Image<th>
  24. </tr>
  25. </thead>
  26. <tbody>
  27. <?php
  28. $conn = new mysqli("localhost", "root", "", "db_image") or die(mysqli_error());
  29. $query = $conn->query("SELECT * FROM `photo`") or die(mysqli_error());
  30. while($fetch = $query->fetch_array()){
  31. ?>
  32. <tr>
  33. <td><?php echo $fetch['photo_name']?></td>
  34. <td colspan = "2"><center><?php echo '<img src = "images/'.$fetch['photo_name'].'" width = "150px" height = "70px"/>'?></center></td>
  35. </tr>
  36. <?php
  37. }
  38. ?>
  39. </tbody>
  40. </table>
  41. </div>
  42. </div>
  43. </body>
  44. </html>
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!!

Tags

Add new comment