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

AngularJS Upload with PHP/MySQLi

PetaPaka123

Next-Gen Tech Pioneer
P Rep
0
0
0
Rep
0
P Vouches
0
0
0
Vouches
0
Posts
127
Likes
83
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 200 XP
Getting Started

I've used CDN for Bootstrap and Angular JS so you need internet connection for them to work.

Creating our Database

First, we are going to create our database.

1. Open phpMyAdmin.
2. Click databases, create a database and name it as angular.
3. After creating a database, click the SQL and paste the below codes. See image below for detailed instruction.

  1. CREATE

    TABLE

    `upload`

    (
  2. `imageid`

    int

    (

    11

    )

    NOT

    NULL

    AUTO_INCREMENT

    ,
  3. `filename`

    varchar

    (

    200

    )

    NOT

    NULL

    ,
  4. PRIMARY KEY

    (

    `imageid`

    )
  5. )

    ENGINE

    =

    InnoDB

    DEFAULT

    CHARSET

    =

    latin1;

database_6_40.png

Creating our Output Folder

Next, we're going to create a folder which will contain our uploaded files.

In the root directory of our project, create a new folder and name it as upload.

index.php

Next, we're going to create our index which contains our upload form
and displays our uploaded files, in the case of this tutorial, uploaded images.

  1. <!DOCTYPE html>
  2. <html

    >
  3. <head

    >
  4. <title

    >

    AngularJS Upload with PHP/MySQLi</

    title

    >
  5. <meta

    charset

    =

    "utf-8"

    >
  6. <link

    href

    =

    "https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"

    rel

    =

    "stylesheet"

    >
  7. <script

    src

    =

    "http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"

    ></

    script

    >
  8. </

    head

    >
  9. <body

    ng-app=

    "app"

    ng-controller=

    "uploader"

    ng-init=

    "fetch()"

    >
  10. <div

    class

    =

    "container"

    >
  11. <h1

    class

    =

    "page-header text-center"

    >

    AngularJS Upload with PHP/MySQLi</

    h1

    >
  12. <div

    class

    =

    "row"

    >
  13. <div

    class

    =

    "col-md-3"

    >
  14. <h3

    class

    =

    "text-center"

    >

    Upload File</

    h3

    >
  15. <hr

    >
  16. <label

    >

    File:</

    label

    >
  17. <input

    type

    =

    "file"

    file-input=

    "files"

    >
  18. <hr

    >
  19. <button

    class

    =

    "btn btn-primary"

    ng-click=

    "upload()"

    ><span

    class

    =

    "glyphicon glyphicon-download-alt"

    ></

    span

    >

    Upload</

    button

    >
  20. <div

    ng-show=

    "error"

    class

    =

    "alert alert-danger text-center"

    style

    =

    "margin-top:30px;"

    >
  21. <button

    type

    =

    "button"

    class

    =

    "close"

    ng-click=

    "clearMessage()"

    ><span

    aria-hidden=

    "true"

    >

    &times;

    </

    span

    ></

    button

    >
  22. <span

    class

    =

    "glyphicon glyphicon-remove"

    ></

    span

    >

    {{ errorMessage }}
  23. </

    div

    >
  24. <div

    ng-show=

    "success"

    class

    =

    "alert alert-success text-center"

    style

    =

    "margin-top:30px;"

    >
  25. <button

    type

    =

    "button"

    class

    =

    "close"

    ng-click=

    "clearMessage()"

    ><span

    aria-hidden=

    "true"

    >

    &times;

    </

    span

    ></

    button

    >
  26. <span

    class

    =

    "glyphicon glyphicon-check"

    ></

    span

    >

    {{ successMessage }}
  27. </

    div

    >
  28. </

    div

    >
  29. <div

    class

    =

    "col-md-9"

    >
  30. <div

    class

    =

    "col-md-4"

    ng-repeat=

    "image in images"

    >
  31. <img

    ng-src

    =

    "upload/{{ image.filename }}"

    width

    =

    "100%"

    height

    =

    "250px"

    class

    =

    "thumbnail"

    >
  32. </

    div

    >
  33. </

    div

    >
  34. </

    div

    >
  35. </

    div

    >
  36. <script

    src

    =

    "angular.js"

    ></

    script

    >
  37. </

    body

    >
  38. </

    html

    >

angular.js

This is contains our Angular JS code in fetching data from database and uploading data into our database.

  1. var

    app =

    angular.module

    (

    'app'

    ,

    [

    ]

    )

    ;
  2. app.directive

    (

    'fileInput'

    ,

    function

    (

    $parse)

    {
  3. return

    {
  4. restrict:

    'A'

    ,
  5. link:

    function

    (

    $scope,

    element,

    attrs)

    {
  6. element.on

    (

    'change'

    ,

    function

    (

    e)

    {
  7. var

    files =

    e.target

    .files

    ;
  8. $parse(

    attrs.fileInput

    )

    .assign

    (

    $scope,

    element[

    0

    ]

    .files

    )

    ;
  9. $scope.$apply(

    )

    ;
  10. }

    )

    ;
  11. }
  12. }
  13. }

    )

    ;
  14. app.controller

    (

    'uploader'

    ,

    function

    (

    $scope,

    $http)

    {
  15. $scope.error

    =

    false

    ;
  16. $scope.errorMessage

    =

    ""

    ;
  17. $scope.success

    =

    false

    ;
  18. $scope.successMessage

    =

    ""

    ;
  19. $scope.upload

    =

    function

    (

    )

    {
  20. var

    uploadForm =

    new

    FormData(

    )

    ;
  21. angular.forEach

    (

    $scope.files

    ,

    function

    (

    file)

    {
  22. uploadForm.append

    (

    'file'

    ,

    file)

    ;
  23. }

    )

    ;
  24. $http.post

    (

    'upload.php'

    ,

    uploadForm,

    {
  25. transformRequest:

    angular.identity

    ,
  26. headers:

    {

    'Content-Type'

    :

    undefined

    ,

    'Process-Data'

    :

    false

    }
  27. }

    )

    .success

    (

    function

    (

    response)

    {
  28. if

    (

    response.error

    )

    {
  29. $scope.error

    =

    true

    ;
  30. $scope.errorMessage

    =

    response.message

    ;
  31. }
  32. else

    {
  33. $scope.success

    =

    true

    ;
  34. $scope.successMessage

    =

    response.message

    ;
  35. $scope.fetch

    (

    )

    ;
  36. }
  37. }

    )
  38. }

    ,

  39. $scope.fetch

    =

    function

    (

    )

    {
  40. $http.get

    (

    'fetch.php'

    )
  41. .success

    (

    function

    (

    data)

    {
  42. $scope.images

    =

    data;
  43. }

    )

    ;

  44. }

  45. $scope.clearMessage

    =

    function

    (

    )

    {
  46. $scope.error

    =

    false

    ;
  47. $scope.errorMessage

    =

    ""

    ;
  48. $scope.success

    =

    false

    ;
  49. $scope.successMessage

    =

    ""

    ;
  50. }
  51. }

    )

    ;

fetch.php

This is our PHP code in fetching the uploaded files in our database.

  1. <?php
  2. $conn

    =

    new

    mysqli(

    'localhost'

    ,

    'root'

    ,

    ''

    ,

    'angular'

    )

    ;
  3. $output

    =

    array

    (

    )

    ;
  4. $sql

    =

    "SELECT * FROM upload"

    ;
  5. $query

    =

    $conn

    ->

    query

    (

    $sql

    )

    ;
  6. while

    (

    $row

    =

    $query

    ->

    fetch_array

    (

    )

    )

    {
  7. $output

    [

    ]

    =

    $row

    ;
  8. }

  9. echo

    json_encode

    (

    $output

    )

    ;
  10. ?>

upload.php

Lastly, this contains our PHP code in uploading files into our database.

  1. <?php

  2. $conn

    =

    new

    mysqli(

    'localhost'

    ,

    'root'

    ,

    ''

    ,

    'angular'

    )

    ;
  3. $out

    =

    array

    (

    'error'

    =>

    false

    )

    ;
  4. if

    (

    !

    empty

    (

    $_FILES

    )

    )

    {
  5. $newFilename

    =

    time

    (

    )

    .

    "_"

    .

    $_FILES

    [

    'file'

    ]

    [

    'name'

    ]

    ;
  6. $path

    =

    'upload/'

    .

    $newFilename

    ;
  7. if

    (

    move_uploaded_file

    (

    $_FILES

    [

    'file'

    ]

    [

    'tmp_name'

    ]

    ,

    $path

    )

    )

    {
  8. $sql

    =

    "INSERT INTO upload (filename) VALUES ('$newFilename

    ')"

    ;
  9. $query

    =

    $conn

    ->

    query

    (

    $sql

    )

    ;
  10. if

    (

    $query

    )

    {
  11. $out

    [

    'message'

    ]

    =

    'File Uploaded Successfully'

    ;
  12. }
  13. else

    {
  14. $out

    [

    'error'

    ]

    =

    true

    ;
  15. $out

    [

    'message'

    ]

    =

    'File Uploaded but not Saved'

    ;
  16. }
  17. }
  18. }
  19. else

    {
  20. $out

    [

    'error'

    ]

    =

    true

    ;
  21. $out

    [

    'message'

    ]

    =

    'Upload Failed. File empty!'

    ;
  22. }

  23. echo

    json_encode

    (

    $out

    )

    ;
  24. ?>

That ends this tutorial. Happy Coding :)


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

452,292

323,526

323,535

Top