简体   繁体   中英

Angular js file upload to php server

I am developing an e-commerce site where a logged in admin needs to add new a product. The product form has product description and product image to upload. I want to catch all form data in an angular controller and pass them to a service to to process. So far I have failed.

Here is the HTML markup:

<label for="product_name">Product name</label>
<input id="product_name" type="text" ng-model="addprovm.product.name">
<input id="product_img" type="file" ng-model="file" name="file">

The form is wrapper in an angular function. ng-submit="addprovm.CreateProduct()"

In the my controller I have following:

function addproductCtrl($http){
    var     addprovm        =   this;               

    // product details.
    addprovm.product = {
        name: "",
        price: "",
        category: "",
        product_img: ""
    };

    
    // submit button function.
    addprovm.CreateProduct  =   function(){         
       console.log(addprovm.product);
    }
}

Here I wanted to console.log the form data and validate (if necessary) before I send them to a service, which then takes this data and passes it to a php script to process.

How can I make my php script upload the image file to a location, and insert product info to a mysql db?

This a simple upload file, maybe can help you, return an object with the status of the file. You can add a function to call this file before the INSERT query.

<?php include_once("../conection.php");?>
<?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("jpg","jpeg","png");

        if(in_array($file_ext, $extensions) === false){
            $errors[]="Is not a valid File.";
        }

        $path = dirname(__FILE__) . '/products/';

        if(file_exists($path.'/'.$file_name)) {
            $errors[] = "This file has already been uploaded.";
        }

        if (!file_exists($path)) {
           mkdir($path, 0777, true);
        }

        if(empty($errors) == true){
            move_uploaded_file($file_tmp, $path.'/'.$file_name);
            echo (json_encode(['file' => $file_name]));
        }else{
            echo(json_encode(['errores' => $errors]));
        }
    }
?>

You can try ng-file-upload ;) and is easy to use

$scope.upload = function (file) {
        Upload.upload({
            url: 'upload/url',
            data: {file: file, 'username': $scope.username}
        }).then(function (resp) {
            console.log('Success ' + resp.config.data.file.name + 'uploaded. Response: ' + resp.data);
        }, function (resp) {
            console.log('Error status: ' + resp.status);
        }, function (evt) {
            var progressPercentage = parseInt(100.0 * evt.loaded / evt.total);
            console.log('progress: ' + progressPercentage + '% ' + evt.config.data.file.name);
        });
    };

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM