Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JS Byte Array to File on PHP Side

I have a Uint8Array which have 10000 bytes on Javascript side.

var data = new Uint8Array(10000) ;

I want to send it to PHP and create a file with it:

    $.ajax({
        url:'saver.php',
        type: 'POST',
        contentType: 'application/octet-stream',  
        data:data,
        processData: false
    });

This ajax is sending data (I see it on "Request Payload" section on console) but there is no $_POST record, $_POST array is empty, just silent. How to grab it properly?

like image 764
Digerkam Avatar asked Aug 07 '26 08:08

Digerkam


2 Answers

This seems to be a known behavior. For an array of 10000 bytes, you should be able to use

$rawPost = file_get_contents('php://input');

to get your data from the request instead of checking the $_POST variable.

Note that this method loads the data in-memory, so if the data are too large, you will get an error.

like image 189
Cithel Avatar answered Aug 08 '26 22:08

Cithel


In my case. I get binary file from JS request array : $file['content'] format of Uint8Array.

            <?php
            $packed = pack("c*", ...$file['content']);
like image 31
Ruslan Novikov Avatar answered Aug 08 '26 20:08

Ruslan Novikov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!