Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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.

share|improve this question

2 Answers 2

up vote 1 down vote accepted

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);
}
?>
share|improve this answer
    
next time , just try to debug with a var_dump ($_FILES) before if conditions –  MouradK Nov 21 at 11:22

I'm using the same uploader for my php application , my uploader works just fine but it doesn't show any progress or abort/cancel button! do you have your progress bar?! how did you use it?

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.