【问题标题】:AngularJS Image upload using phpAngularJS 使用 php 上传图片
【发布时间】:2014-07-11 12:16:51
【问题描述】:

我在 AngularJS 中上传图片时遇到问题。我在这里发现了这个问题:Angularjs - File upload with php

与其他问题一样,我尝试使用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 的新手,很抱歉有任何菜鸟问题。

【问题讨论】:

  • 你好,你能给我一个 Angular 2/4 的示例/链接,用于将多个图像上传到服务器。

标签: javascript php jquery angularjs


【解决方案1】:

我的 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);
}
?>

【讨论】:

  • 下一次,在 if 条件之前尝试使用 var_dump ($_FILES) 进行调试
  • 你能把你的整个 html 贴出来吗?
猜你喜欢
  • 2015-06-14
  • 2018-06-04
  • 2015-10-02
  • 2019-05-19
  • 2018-11-11
  • 2016-07-24
  • 2013-06-17
  • 2020-05-02
  • 2010-09-26
相关资源
最近更新 更多