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

PHP - Simple Image Upload Preview Using AngularJS

profitnet2k

Tactical Genius
P Rep
0
0
0
Rep
0
P Vouches
0
0
0
Vouches
0
Posts
110
Likes
142
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 300 XP
In this tutorial we will create a Simple Image Upload Preview Using AngularJS. This code will display a preview before sending the file to the database server. The uploaded image file is being process by angular directives that act as a POST request This a user-friendly program feel free to modify and use it to your system.

We will be using AngularJS as a framework which has additional custom HTML attributes embedded into it. It can interprets those attributes as directives to bind inputted parts of the page to a model that represent as a standard JavaScript variables.

Getting Started:

First you have to download & install XAMPP or any local server that run PHP scripts. Here's the link for XAMPP server https://www.apachefriends.org/index.html.

And this is the link for the bootstrap that i used for the layout design https://getbootstrap.com/.

Lastly, this is the link for the AngularJS https://angularjs.org/.

Creating Database

Open your database web server then create a database name in it db_preview, after that click Import then locate the database file inside the folder of the application then click ok.
2019-03-08_22_40_03-localhost_127.0.0.1_db_preview_phpmyadmin_4.8.3.png

Creating the database connection

Open your any kind of text editor(notepad++, etc..). Then just copy/paste the code below then name it conn.php.
  1. <?php
  2. $conn

    =

    mysqli_connect

    (

    "localhost"

    ,

    "root"

    ,

    ""

    ,

    "db_preview"

    )

    ;

  3. if

    (

    !

    $conn

    )

    {
  4. die

    (

    "Error: Failed to connect to database!"

    )

    ;
  5. }
  6. ?>

Creating The Interface

This is where we will create a simple form for our application. To create the forms simply copy and write it into you text editor, then save it index.php.
  1. <!

    DOCTYPE html>
  2. <

    html ng-

    app=

    "myModule"

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

    controller=

    "myController"

    >
  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

    =

    "col-md-3"

    ></

    div>
  14. <

    div class

    =

    "col-md-6 well"

    >
  15. <

    h3 class

    =

    "text-primary"

    >

    PHP -

    Simple Image Upload Preview Using AngularJS</

    h3>
  16. <

    hr style =

    "border-top:1px dotted #000;"

    />
  17. <

    div class

    =

    "col-md-6"

    >
  18. <

    form ng-

    submit=

    "submit()"

    name=

    "form"

    role=

    "form"

    >
  19. <

    div class

    =

    "form-inline"

    >
  20. <

    input ng-

    model=

    "form.image"

    type=

    "file"

    accept=

    "image/*"

    onchange=

    "angular.element(this).scope().uploadedFile(this)"

    >
  21. <

    br />
  22. <

    button class

    =

    "btn btn-primary"

    ><

    span class

    =

    "glyphicon glyphicon-upload"

    ></

    span>

    Upload</

    button>
  23. </

    div>
  24. <

    br/>
  25. </

    form>
  26. </

    div>
  27. <

    div class

    =

    "col-md-6"

    >
  28. <

    center><

    img ng-

    src=

    "{{image_source}}"

    style=

    "width:250px; height:200px;"

    ></

    center>
  29. </

    div>
  30. </

    div>
  31. </

    body>
  32. <

    script src=

    "js/angular.js"

    >

    </script>
  33. <

    script src =

    "js/script.js"

    >

    </script>
  34. </

    html>

Creating the Main Function

This code contains the main function of the application. This code will display a preview for the uploaded file before saving it. To do this just copy and write these block of codes as shown below inside the text editor and save it as shown below.
upload.php
  1. <?php
  2. require_once

    'conn.php'

    ;

  3. if

    (

    !

    empty

    (

    $_FILES

    [

    'image'

    ]

    )

    )

    {
  4. $file_name

    =

    $_FILES

    [

    'image'

    ]

    [

    'name'

    ]

    ;
  5. $file_temp

    =

    $_FILES

    [

    'image'

    ]

    [

    'tmp_name'

    ]

    ;

  6. $exp

    =

    explode

    (

    "."

    ,

    $file_name

    )

    ;
  7. $ext

    =

    end

    (

    $exp

    )

    ;
  8. $image

    =

    time

    (

    )

    .

    '.'

    .

    $ext

    ;
  9. $ext_allowed

    =

    array

    (

    "png"

    ,

    "gif"

    ,

    "jpeg"

    ,

    "jpg"

    )

    ;
  10. $location

    =

    "uploads/"

    .

    $image

    ;
  11. if

    (

    in_array

    (

    $ext

    ,

    $ext_allowed

    )

    )

    {
  12. move_uploaded_file

    (

    $file_temp

    ,

    $location

    )

    ;
  13. mysqli_query

    (

    $conn

    ,

    "INSERT INTO `image` (image, location) VALUES('$image

    ', '$location

    ')"

    )

    or die

    (

    mysqli_error

    (

    )

    )

    ;
  14. echo

    "Successfully uploaded!"

    ;
  15. }

    else

    {
  16. echo

    "Invalid image format"

    ;
  17. }
  18. }

    else

    {
  19. echo

    "Please upload image first"

    ;
  20. }
  21. ?>

script.js
Note: Make sure you save this file inside the js directory in order the script works.
  1. var

    app =

    angular.module

    (

    'myModule'

    ,

    [

    ]

    )

    ;

  2. app.controller

    (

    'myController'

    ,

    function

    (

    $scope,

    $http)

    {

  3. $scope.form

    =

    [

    ]

    ;
  4. $scope.files

    =

    [

    ]

    ;


  5. $scope.uploadedFile

    =

    function

    (

    element)

    {
  6. $scope.currentFile

    =

    element.files

    [

    0

    ]

    ;
  7. var

    reader =

    new

    FileReader(

    )

    ;
  8. reader.onload

    =

    function

    (

    event)

    {
  9. $scope.image_source

    =

    event.target

    .result
  10. $scope.$apply(

    function

    (

    $scope)

    {
  11. $scope.files

    =

    element.files

    ;
  12. }

    )

    ;
  13. }
  14. reader.readAsDataURL

    (

    element.files

    [

    0

    ]

    )

    ;
  15. }

  16. $scope.submit

    =

    function

    (

    )

    {
  17. $scope.form

    .image

    =

    $scope.files

    [

    0

    ]

    ;

  18. $http(

    {
  19. method :

    'POST'

    ,
  20. url :

    'upload.php'

    ,
  21. processData:

    false

    ,
  22. transformRequest:

    function

    (

    data)

    {
  23. var

    formData =

    new

    FormData(

    )

    ;
  24. formData.append

    (

    "image"

    ,

    $scope.form

    .image

    )

    ;
  25. return

    formData;
  26. }

    ,
  27. data :

    $scope.form

    ,
  28. headers:

    {
  29. 'Content-Type'

    :

    undefined
  30. }
  31. }

    )

    .then

    (

    function

    (

    data)

    {
  32. alert(

    data.data

    )

    ;
  33. }

    )

    ;

  34. }

    ;
  35. }

    )

    ;

There you have it we successfully created a Simple Image Upload Preview using AngularJS. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!!


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

450,491

323,080

323,089

Top