AngularJS 使用 php 上传图像

2024-03-24

我在 AngularJS 中上传图像时遇到问题。我在这里发现了这个问题:Angularjs - 使用 php 上传文件 https://stackoverflow.com/questions/20487212/angularjs-file-upload-with-php

正如在另一个问题中我尝试使用https://github.com/danialfarid/angular-file-upload https://github.com/danialfarid/angular-file-upload

我的问题是我尝试上传的图像没有发送到我的 php 文件。

这是我使用的代码。

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>

上传.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);
}
?>

因此“if(isset($_FILES['image']))”给出 false 结果。我是 stackoverflow 和 angularJS 的新手,很抱歉有任何菜鸟问题。


我的 PHP 遇到了问题。问题在于 $_FILES['image'] 图像应该是文件 本来应该是:

<?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);
}
?>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

AngularJS 使用 php 上传图像 的相关文章

随机推荐