简体   繁体   中英

How to access file from multipart/form-data [ PHP ]

I have a frontend of my application writen in angular which basicly pick the file and send backend file name and rest of the form information:

Frontend part looks like:

$scope.submit = function() {
    if ($scope.file) {
        $scope.upload($scope.file);
    }
};

// upload on file select or drop
$scope.upload = function (file) {
    Upload.upload({
        url: 'sub.php',
        data: {file: file, 'country': $scope.submodelCountry}
    }).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);
    });
};

My backend is written in PHP: I basicly access this 2 variabiles by:

$fileName = $_FILES['file']['name']; // value = fileName.csv
$country = $_POST['country']; // value = CZE

So looks my backend get all needed information however I am not able to fopen the file with $file = fopen($fileName,"r"); How can I access this file guys? Or how can I upload it on a server to use it after?

you have to upload the file to some folder, then you can access that,

Your file will be present in tmp directory, so

Use move_uploaded_files() , to move your file,

<?php
    $uploads_dir = '/uploads';
    if(move_uploaded_file($_FILES["file"]["tmp_name"],
  "$uploads_dir/".$_FILES["file"]["file_name"])){
         echo "file uploaded";
    }
?>

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