Php – Upload image to server path using AngularJS and PHP

angularjsimagePHPupload

As the question mentioned, I have a question regarding how to upload image to server. I have searched through the internet and found some reference but I still don't understand as how they work. For example: AngularJS Image upload using php

How is the image being uploaded? As there are no submit button there. I have followed the code there and tried it, still can't upload the files.

I have also tried to see some available modules from here :File Upload using AngularJS
After looking for couple of hours, I still don't understand how to use them. Is there any simpler or easier understand guides for me to follow? Thanks in advance.

Edit: If there are simpler example like just upload one file, please link me to it as there are many examples that are quite advance which consist of many parts such as upload multiple file etc.

Best Answer

try this for html

<div type="file" ngf-drop ng-model="files" class="container drop-box text-center" 
ngf-drag-over-class="dragover" ngf-multiple="true" ngf-allow-dir="true"
ngf-accept="'.jpg,.png'"><input type="hidden" name="image_id" >Drop Images here</div>

try this in you controller

controller: function($scope,$http,$timeout,Upload) {

        $scope.$watch('files', function () {
             $scope.upload($scope.files);
        });

        $scope.upload = function (files) {
            if (files && files.length) {
                for (var i = 0; i < files.length; i++) {
                    var file = files[i];
                    Upload.upload({
                        url: 'php/upload.php', 
                        headers: {'Content-Type': file.type},
                        method: 'POST',
                        data: file,
                        file: file, 
                    }).progress(function (evt) {
                        var progressPercentage = parseInt(100.0 * evt.loaded / evt.total);
                        console.log('progress: ' + progressPercentage + '% ' + evt.config.file.name);
                    }).success(function (data, status, headers, config) {
                        console.log('file ' + config.file.name + 'uploaded. Response: ' + data);
                        $scope.image_submit = function() {
            $http.post('php/slider.php?action=add_image', 
            {
            'img_name' : config.file.name, 
            'img_src' : '../images/' + config.file.name
            }
            )
            .success(function (data, status, headers, config) {
            $scope.get_image();
            })

            .error(function(data, status, headers, config){

            });
            };
            $scope.image_submit();
                    });
                }
            }
        };

and this in your php file

<?php

$filename = $_FILES['file']['name'];
$destination = '../images/' . $filename;
move_uploaded_file( $_FILES['file']['tmp_name'] , $destination );

?>

You also need to create a db connection and create 2 php functions one to grab the images from the database and one to create the image and one function in your controller to get the image.The function to add image i have already give it to you in the controller.