FormData无法在php文件中访问

时间:2018-03-31 15:43:24

标签: php angularjs form-data

我使用angular和php创建了一个文件上传。我正在使用FormData将文件发送到后端。文件附加在UI端但无法使用 $_FILES['file']['name']

  

error_log:未定义的索引:文件输入   第3行/home/yrpj2p9qt7gb/public_html/upload.php

以下是我的代码。

index.html with ng-file =' uploadfiles'作为自定义指令

<form method="POST" enctype="multipart/form-data">
            <label class="dash-form_label">Image</label>
            <input type='file' name='file' id='file' ng-file='uploadfiles'>
            <br>
            <input type='button' value='Upload' id='upload' ng-click='upload()'>

        </form>

app.js

app.controller('dashboardCtrl', DashboardCtrl);
app.directive('ngFile', ['$parse', function ($parse) {
    return {
        restrict: 'A',
        link: function (scope, element, attrs) {
            console.log(scope, element, attrs);
            element.bind('change', function () {
                $parse(attrs.ngFile).assign(scope, element[0].files)
                scope.$apply();
            })
        }
    }
}]);

function DashboardCtrl($scope, $http) {
    $scope.upload = function () {
        console.log($scope.uploadfiles);
        var fd = new FormData();
        angular.forEach($scope.uploadfiles, function (File) {
            fd.append('file', File);
        });
        console.log(fd.get('file'));

        $http({
            method: 'post',
            url: '../../upload.php',
            data: fd,
            header: { 'Content-Type': undefined }
        }).then(function successCallback(response) {
            $scope.response = response.data;
            console.log($scope.response);
        }, function (err) {
            console.log(err);
        })
    };
};

upload.php的

<?php
/* Getting file name */
$filename = $_FILES['file']['name'];

/* Location */
$location = 'upload/';

/* Upload file */
move_uploaded_file($_FILES['file']['tmp_name'],$location.$filename);

$arr = array("name"=>$filename);
echo json_encode($arr);

0 个答案:

没有答案