2014-05-22 29 views
5

Tôi gặp sự cố khi tải hình ảnh lên AngularJS. Tôi tìm thấy câu hỏi này ở đây: Angularjs - File upload with phpAngularJS Tải lên hình ảnh bằng cách sử dụng php

Như trong câu hỏi khác tôi cố gắng sử dụng https://github.com/danialfarid/angular-file-upload

Vấn đề của tôi là hình ảnh của tôi mà tôi cố gắng để tải lên không được gửi đến tập tin php của tôi.

Đây là mã mà tôi sử dụng.

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

Vì vậy, các "if (isset ($ _ FILES [ 'image']))" cho giả là kết quả. Tôi mới để stackoverflow và angularJS rất xin lỗi cho bất kỳ câu hỏi noob.

+0

Xin chào, bạn có thể cung cấp cho tôi một ví dụ/link cho góc 2/4 để tải lên hình ảnh muliple đến máy chủ . –

Trả lời

9

Tôi gặp sự cố trong PHP của mình. Vấn đề là với $ _FILES [ 'image'] hình ảnh cần phải có được tập tin Nó cần phải có được:

<?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); 
} 
?> 
+1

lần sau, chỉ cần thử gỡ lỗi bằng var_dump ($ _FILES) trước nếu điều kiện – MouradK

+0

bạn có thể đăng toàn bộ html của mình không? – Zypps987

Các vấn đề liên quan