AngularJS Image upload using php

All we need is an easy explanation of the problem, so here it is.

I’ve got a problem with an image upload in AngularJS. I found this question on here: Angularjs – File upload with php

As in the other question I try to use https://github.com/danialfarid/angular-file-upload

My problem is that my image that I try to upload isn’t send to my php file.

Here is the code that I use.

PlayerController.js

angular.module('lax').controller('PlayerController', function($scope, $http, $upload) {

$scope.onFileSelect = function($files) {
    $scope.message = "";
    for (var i = 0; i < $files.length; i++) {
        var file = $files[i];
        console.log(file);
        $scope.upload = $upload.upload({
            url: 'php/upload.php',
            method: 'POST',               
            file: file
        }).success(function(data, status, headers, config) {
            $scope.message = data;                
        }).error(function(data, status) {
            $scope.message = data;
        });
    }
};
});

HTML

<div ng-show="newplayer.functie == 'update'">
                        <h3>Profile Pic</h3>                         
                        <div>
                            <input type="file" name="image" id="image" ng-file-select="onFileSelect($files)">
                            <br/>
                            <span class="errorMsg">{{ message}}</span>
                        </div>

                    </div>

upload.php

<?php
    if(isset($_FILES['image'])){    
        $errors= array();        
        $file_name = $_FILES['image']['name'];
        $file_size =$_FILES['image']['size'];
        $file_tmp =$_FILES['image']['tmp_name'];
        $file_type=$_FILES['image']['type'];   
        $file_ext = strtolower(pathinfo($file_name, PATHINFO_EXTENSION));
        $extensions = array("jpeg","jpg","png");        
        if(in_array($file_ext,$extensions )=== false){
             $errors[]="image extension not allowed, please choose a JPEG or PNG file.";
        }
        if($file_size > 2097152){
            $errors[]='File size cannot exceed 2 MB';
        }               
        if(empty($errors)==true){
            move_uploaded_file($file_tmp,"../../Img/PlayerAvatar/".$file_name);
            echo $fname . " uploaded file: " . "images/" . $file_name;
        }else{
            print_r($errors);
        }
  }
    else{
        $errors= array();
        $errors[]="No image found";
        print_r($errors);
}
?>

So the “if(isset($_FILES[‘image’]))” gives false as a result. I’m new to stackoverflow and angularJS so sorry for any noob questions.

How to solve :

I know you bored from this bug, So we are here to help you! Take a deep breath and look at the explanation of your problem. We have many solutions to this problem, But we recommend you to use the first method because it is tested & true method that will 100% work for you.

Method 1

I had a problem in my PHP. The problem was with the $_FILES[‘image’] image should have been file
It should have been:

<?php
    if(isset($_FILES['file'])){    
    $errors= array();        
    $file_name = $_FILES['file']['name'];
    $file_size =$_FILES['file']['size'];
    $file_tmp =$_FILES['file']['tmp_name'];
    $file_type=$_FILES['file']['type'];   
    $file_ext = strtolower(pathinfo($file_name, PATHINFO_EXTENSION));
    $extensions = array("jpeg","jpg","png");        
    if(in_array($file_ext,$extensions )=== false){
         $errors[]="image extension not allowed, please choose a JPEG or PNG file.";
    }
    if($file_size > 2097152){
        $errors[]='File size cannot exceed 2 MB';
    }               
    if(empty($errors)==true){
        move_uploaded_file($file_tmp,"PlayerAvatar/".$file_name);
        echo " uploaded file: " . "images/" . $file_name;
    }else{
        print_r($errors);
    }
}
else{
    $errors= array();
    $errors[]="No image found";
    print_r($errors);
}
?>

Note: Use and implement method 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply