• Register now to get access to thousands of Tutorials, Leaked content, Hot NSFW and much more. Join us as we build and grow the community.

Advertise Here

Advertise Here

Advertise Here

Simple Image Upload Using AngularJS/PHP

FLux112

Recursive Algorithm Developer
F Rep
0
0
0
Rep
0
F Vouches
0
0
0
Vouches
0
Posts
171
Likes
130
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 2 1000 XP
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`

    )
  5. )

    ENGINE

    =

    InnoDB

    DEFAULT

    CHARSET

    =

    latin1 AUTO_INCREMENT

    =

    3

    ;

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. app.controller

    (

    'myController'

    ,

    function

    (

    $scope,

    $http)

    {

  3. $scope.form

    =

    [

    ]

    ;
  4. $scope.files

    =

    [

    ]

    ;

  5. $scope.submit

    =

    function

    (

    )

    {
  6. $scope.form

    .image

    =

    $scope.files

    [

    0

    ]

    ;

  7. $http(

    {
  8. method :

    'POST'

    ,
  9. url :

    'upload.php'

    ,
  10. processData:

    false

    ,
  11. transformRequest:

    function

    (

    data)

    {
  12. var

    formData =

    new

    FormData(

    )

    ;
  13. formData.append

    (

    "image"

    ,

    $scope.form

    .image

    )

    ;
  14. return

    formData;
  15. }

    ,
  16. data :

    $scope.form

    ,
  17. headers:

    {
  18. 'Content-Type'

    :

    undefined
  19. }
  20. }

    )

    .then

    (

    function

    (

    data)

    {
  21. alert(

    "Successfully Upload An Image!"

    )

    ;
  22. window.location

    =

    "index.php"

    ;
  23. }

    )

    ;

  24. }

    ;

  25. $scope.uploadedFile

    =

    function

    (

    element)

    {
  26. $scope.currentFile

    =

    element.files

    [

    0

    ]

    ;
  27. var

    reader =

    new

    FileReader(

    )

    ;
  28. reader.onload

    =

    function

    (

    event)

    {
  29. $scope.image_source

    =

    event.target

    .result
  30. $scope.$apply(

    function

    (

    $scope)

    {
  31. $scope.files

    =

    element.files

    ;
  32. }

    )

    ;
  33. }
  34. reader.readAsDataURL

    (

    element.files

    [

    0

    ]

    )

    ;
  35. }
  36. }

    )

    ;

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!!


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

Create an account or login to comment

You must be a member in order to leave a comment

Create account

Create an account on our community. It's easy!

Log in

Already have an account? Log in here.

452,499

349,821

349,831

Top