5

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.

1
  • Hello, can you provide me a example/link for angular 2/4 for uploading muliple images to server. Commented Feb 7, 2018 at 8:59

1 Answer 1

8

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);
}
?>
Sign up to request clarification or add additional context in comments.

2 Comments

next time , just try to debug with a var_dump ($_FILES) before if conditions
can you post the entirety of your html?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.