0

Please note this is not a straightforward HTML->PHP file uploading issue. This is a part of a much bigger AngularJS app so the file has to be uploaded via AngularJS interface. When AngularJS script sends an image file to Php the file seems empty. In PHP sizeof($_FILES) returns 0. Any ideas? Thanks!

HTML code:

<div ng-app="myApp" ng-controller="myCtrl">
    <form name="myForm">
        <div>
        <input type="file" ngf-select="onFileSelect(file)" name="file">
            <span ng-show="fileMsg">{{fileMsg}}</span>
        </div>
    </form>
</div>

AngularJS code:

var app = angular.module('myApp', ['ngFileUpload']);
app.controller('myCtrl', function($scope, $http) {
  $scope.fileMsg = "Checkpoint 1";
  $scope.onFileSelect = function(file) {
        var fd = new FormData();
         fd.append('file', file);
         $http.post("save_img.php", fd, {
             transformRequest: angular.identity,
             headers: {'Content-Type': undefined}
        })
        .then(function(response){
             $scope.fileMsg = response.data;
        });
  }
});

PHP code:

echo $_FILES." size: ".sizeof($_FILES);
Cindy
  • 31
  • 4
  • Possible duplicate of [how to upload a file to my server using html](https://stackoverflow.com/questions/5628011/how-to-upload-a-file-to-my-server-using-html) – Peter Apr 14 '18 at 13:58
  • 1
    Forms with file inputs need an enctype="multipart/form-data" attribute to work correctly. – Peter Apr 14 '18 at 13:59
  • you can check if the image has been uploaded or not using [is_uploaded_file](http://php.net/manual/en/function.is-uploaded-file.php) `if(is_uploaded_file($_FILES['file']['tmp_name'])) { echo 'Uploaded!'; }` – Amr Aly Apr 14 '18 at 14:00
  • Uploading a file following the HTML->PHP route is straightforward BUT my issue is to upload a file via an AngularJS interface, hence, the path becomes HTML->AngularJS->PHP. The "how to upload a file using html" didn't address this issue. Adding enctype="multipart/form-data" didn't work. Thanks for your help but please don't label it as a duplicate too soon. – Cindy Apr 14 '18 at 23:47
  • Amr - thanks for the tips. Testing with it_uploaded_file turned out that the image was not uploaded. – Cindy Apr 14 '18 at 23:49

0 Answers0