一次上传2张图片

时间:2018-06-09 13:29:55

标签: php mysql angular multiple-file-upload

我使用angular 2和PHP将两个文件名上传到MYSQL数据库并将文件复制到特定文件夹中。 我的代码在上传一个文件时正在运行,但我不知道如何更改它以上传两个文件。感谢您提前帮助。

index.php代码是

    <body>  
       <br />  
       <h3 align="center">File Upload Using AngularJS with PHP Script</h3> 
       <br />  
       <br />  
       <div class="container" ng-app="myapp" ng-controller="myController" 
        ng-init="select()">  
            <div class="col-md-4">  
   Select file1<input type="file" file-input="files" /> </br> 
            Select file2<input type="file" file-input="file2" />  
            </div>  
        <div class="col-md-6">  
        <button class="btn btn-info" ng-click="uploadFile()">Upload</button>  
            </div>  
            <div style="clear:both"></div>  
            <br /><br />  

  <div class="col-md-3" ng-repeat="file in files">  
 <img ng-src="upload/{{file.name}}" width="200" height="200" style="padding:16px; border:1px solid #f1f1f1; margin:16px;"  alt="music"/>  



            </div>  
       </div>  
  </body>  



  <script>  
 var app = angular.module("myapp", []);  
 app.directive("fileInput", function($parse){  
  return{  
       link: function($scope, element, attrs){  
            element.on("change", function(event){  
                 var files = event.target.files;  
                 //console.log(files[0].name);  
                 $parse(attrs.fileInput).assign($scope, element[0].files);  
                 $scope.$apply();  
            });  
       }  
       }  
    });  
   app.controller("myController", function($scope, $http){  
   $scope.uploadFile = function(){  
       var form_data = new FormData();  
       angular.forEach($scope.file, function(file){  
            form_data.append('file', file);  
       });  
       $http.post('upload.php', form_data,  
       {  
            transformRequest: angular.identity,  
            headers: {'Content-Type': undefined,'Process-Data': false}  
       }).success(function(response){  
            alert(response);  
            $scope.select();  
       });  
     }  
    $scope.select = function(){  
       $http.get("select.php")  
       .success(function(data){  
            $scope.files = data;  
       });  
      }  
    });  
    </script>  

upload.php文件 在Upload.php中我试图上传我的文件名并将文件移动到特定文件夹

      <?php  
 $connect = mysqli_connect("localhost", "root", "", "testing");  
 if(!empty($_FILES))  
 {  
      $path = 'upload/' . $_FILES['file']['name'];  
      if(move_uploaded_file($_FILES['file']['tmp_name'], $path))  
      {  
           $insertQuery = "INSERT INTO tbl_images(name) VALUES ('".$_FILES['file']['name']."')";  
           if(mysqli_query($connect, $insertQuery))  
           {  
                echo 'File Uploaded';  
           }  
           else  
           {  
                echo 'File Uploaded But not Saved';  
           }  
      }  
 }  
 else  
 {  
      echo 'Some Error';  
 }  
 ?>  

0 个答案:

没有答案