Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.

Join them; it only takes a minute:

Sign up
Join the Stack Overflow community to:
  1. Ask programming questions
  2. Answer and help your peers
  3. Get recognized for your expertise

I am facing a issue.I can not save file into local folder using Angular.js and PHP.I am explaining my code below.

<div class="input-group bmargindiv1 col-md-12">
    <span class="input-group-addon ndrftextwidth text-right" style="width:180px">Upload Image :</span>
    <input type="file"
           class="filestyle form-control"
           data-size="lg"
           name="uploadme"
           id="bannerimage"
           ngf-select
           ng-model="file"
           ngf-pattern="'image/*'"
           accept="image/*"
           ngf-max-size="20MB"
           ngf-min-height="100"
           ngf-resize="{width: 100, height: 100}"
           custom-on-change="uploadFile"
           required="required">
</div>

The above code say my file input field.The controller code for this is given below.

var today = $filter('date')(new Date(), 'yyyy-MM-dd HH:mm:ss');
var newpath = today + "_" + $scope.file.name;
var arrFile = {'name': newpath, 'size': $scope.file.size, 'type': $scope.file.type};
var productdata = $.param({
    'op': 'add',
    'catagory_id': $scope.catagory_name.value,
    'product_name': $scope.product_name,
    'status': $scope.product_status,
    'ucp': $scope.unit_cost_price,
    'usp': $scope.unit_sale_price,
    'quantity': $scope.quantity,
    'specification': $scope.specification,
    'discount': $scope.discount,
    'image': newpath,
    'file': arrFile
});
$http({
    method: 'POST',
    url: "php/productInfo.php",
    data: productdata,
    headers: {'Content-Type': 'application/x-www-form-urlencoded'}
}).then(function successCallback(response) {
}, function errorCallback(response) {
});

Here i have also other form data with file.

productInfo.php:

if ($_REQUEST["op"]=="add") {
    $name     = $_FILES['file']['name'];
    $tmpName  = $_FILES['file']['tmp_name'];
    move_uploaded_file($tmpName,'upload/'.$name);
}

Here i have only mentioned the file uploading part of the php file.I have also upload folder inside my project folder.But i am unable to save the file inside that folder.Please help me to resolve this issue.

share|improve this question
    
print your productdata in productinfo.php file and update the question – saurabh kamble Dec 5 '15 at 5:18
    
@saurabhkamble : Itried to print $_FILES['file']['name']; but its returning nothing.When tried '$_POST['file']['name'];' it returned the file name i have sent. – subhra Dec 5 '15 at 7:12
 **IT WILL WORK FOR YOU**  


<form  role="form">  
div class='form-group'>
              <label class='control-label'>Image</label>
             <input class='form-control' type="file" name="mfile" file-model="getting.image" accept="image/*" resize-max-height="300" resize-max-width="250" resize-quality="0.7" required > 
              <span ng-show="imagerequired" class="help-block">{{imagerequired}}</span>  
</form>

service:-

app.service('multipartForm', ['$http', function($http,$scope,usSpinnerService){
this.post = function(uploadUrl, data){
    var fd = new FormData();
    for(var key in data)
        fd.append(key, data[key]);
    $http.post(uploadUrl, fd, {
        transformRequest: angular.indentity,
        headers: { 'Content-Type': undefined }
    }).success(function(data){

        if(data.RESPONSE_STATUS == "1"){
           // this.responseMessage = "Question added successfully";
          alert("Mantar uploaded successfully...");
            window.location.reload();


            }
            else { 
                alert("Error in Upload Mantra"); 
                //$scope.responseMessage = "Error occur while adding question";

            }
    });
}
this.success = function()
{
    alert("success");
    var success = true;
}
   }]);

directive

app.directive('fileModel', ['$parse', function($parse){
return {
    restrict: 'A',
    link: function(scope, element, attrs){
        var model = $parse(attrs.fileModel);
        var modelSetter = model.assign;

        element.bind('change', function(){
            scope.$apply(function(){
                modelSetter(scope, element[0].files[0]);
            })
        })
    }
}
 }]);

controller :

var uploadUrl = 'upload.php';
        multipartForm.post(uploadUrl, $scope.getting);

php :

 $target_dir = "uploads/".$_FILES['file']['name'];
        move_uploaded_file($_FILES["image"]["tmp_name"], $target_dir);
share|improve this answer
    
I am confusing about your code ? what actually $target_file accepts.Can you please edit your answer in my code ? – subhra Dec 5 '15 at 4:17

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.