sending additional values to PHP script using angular-file-upload

Issue

So i am using angular-file-upload

For this i need the following controller:

app.controller('FileUploadCtrl', ['$scope', 'FileUploader', function($scope, FileUploader) {
var uploader = $scope.uploader = new FileUploader({
    url: 'js/controllers/upload.php',
});

// FILTERS

uploader.filters.push({
    name: 'customFilter',
    fn: function(item /*{File|FileLikeObject}*/, options) {
        return this.queue.length < 10;
    }
});

// CALLBACKS

uploader.onWhenAddingFileFailed = function(item /*{File|FileLikeObject}*/, filter, options) {
    console.info('onWhenAddingFileFailed', item, filter, options);
};
uploader.onAfterAddingFile = function(fileItem) {
    console.info('onAfterAddingFile', fileItem);
};
uploader.onAfterAddingAll = function(addedFileItems) {
    console.info('onAfterAddingAll', addedFileItems);
};
uploader.onBeforeUploadItem = function(item) {
    item.file.test = "gg";
    console.info('onBeforeUploadItem', item);
};
uploader.onProgressItem = function(fileItem, progress) {
    console.info('onProgressItem', fileItem, progress);
};
uploader.onProgressAll = function(progress) {
    console.info('onProgressAll', progress);
};
uploader.onSuccessItem = function(fileItem, response, status, headers) {
    console.info('onSuccessItem', fileItem, response, status, headers);
};
uploader.onErrorItem = function(fileItem, response, status, headers) {
    console.info('onErrorItem', fileItem, response, status, headers);
};
uploader.onCancelItem = function(fileItem, response, status, headers) {
    console.info('onCancelItem', fileItem, response, status, headers);
};
uploader.onCompleteItem = function(fileItem, response, status, headers) {
    console.info('onCompleteItem', fileItem, response, status, headers);
};
uploader.onCompleteAll = function() {
    console.info('onCompleteAll');
};

console.info('uploader', uploader);
}]);

and the following php script:

    <?php

if ( !empty( $_FILES ) ) {

    $tempPath = $_FILES[ 'file' ][ 'tmp_name' ];
    $uploadPath = dirname( __FILE__ ) . DIRECTORY_SEPARATOR . 'uploads' . DIRECTORY_SEPARATOR . $_FILES[ 'file' ][ 'name' ];

    move_uploaded_file( $tempPath, $uploadPath );

    $answer = array( 'answer' => 'File transfer completed' );
    $json = json_encode( $answer );

    echo $json;

} else {

    echo 'No files';

}

?>

Now for some of the files i need to upload i need to specify what path they need to take (which depends on a value in my $sessionStorage)

So my question is how can i send a value with the file to the PHPscript?

Solution

You can send values to PHPscript using formData property:

app.controller('FileUploadCtrl', ['$scope', 'FileUploader', function($scope, FileUploader) {
var uploader = $scope.uploader = new FileUploader({
    url: 'js/controllers/upload.php',
    formData: [{
             param1: 'value1',
             param2: 'value2',
             param3: 'value3'
           }]
});

formData property is an array that will send your data to $_REQUEST in PHP.

So, you can use

$myValue1 = $_REQUEST['param1'];
$myValue2 = $_REQUEST['param2'];
$myValue3 = $_REQUEST['param3'];

for whatever you need.

Answered By – herbert

Answer Checked By – Terry (AngularFixing Volunteer)

Leave a Reply

Your email address will not be published.