How to write a byte string to a file in php?

In js using function readAsBinaryString to get the file part in the form of a byte string, by means of ajax I send it to the php script in which you want to save it. If you save using file_put_contents or (fopen,fwrite,fclose), the string is stored as text. Checked, server comes line as if to open in a hex editor and look at the dump. How to write to a file this byte string?
For the fopen parameter b does not help, still writes as a string(the file is like open source code through a hex editor, copy the dump and paste it into Notepad). Found somewhere that file_put_contents can be run with the FILE_BINARY flag but with the same result.
Under the dump I mean the text representation of bytes that is usually on the right of the hexadecimal view.

So in short I send the file
_ajax_file function(file,path,onprogress,onend,onerror,size){
size=size||2000000;
 var start= 0,
 end=size<file.size?size-1:file.size-1, r=new filereader, _af="function(){" var x=new xmlhttprequest; x.open('post', path, true); x.setrequestheader('content-type', 'application ajax-file'); x.onreadystatechange="function(){" if(x.readystate="=4){" console.log(x.responsetext); if(onprogress) onprogress(start file.size*100); if(start<file.size) _af(); else if(onend) onend(); delete x; } }; r.onload="function(){" if(start<="size)" x.setrequestheader('ajax file','start'); if(start>=file.size)
x.setRequestHeader('ajax file','end');
x.send(this.result);
//console.log(this.result);
};
r.readAsBinaryString(file.slice(start,end));
start=end+1;
 end=size+end<file.size?size+end:file.size; }; _af(); } _ajax_file(this.files[0],location.pathname,function(x){progress.value="x;},function(){progress.value=0;});</code"></file.size?size+end:file.size;></file.size?size-1:file.size-1,>
br><br> So make<br><pre><code lang="php">if($_SERVER["CONTENT_TYPE"]=='application/ajax-file; charset=UTF-8') { $postdata=file_get_contents("php://input"); if($_SERVER['HTTP_AJAX_FILE']=='start') if(file_exists('temp.txt')) unlink('temp.txt'); //file_put_contents('temp.txt',$postdata,FILE_APPEND); $fs=fopen('temp.txt','ab'); fwrite($fs,$postdata); fclose($fs); die(); }</code></pre><br> What am I doing wrong?
September 19th 19 at 12:08
3 answers
September 19th 19 at 12:10
php.net/manual/en/function.file-put-contents.php
Note: This function is binary-safe.
php.net/manual/ru/function.file-put-contents.php - dudley.Ondricka commented on September 19th 19 at 12:13
Note: This function is safe for processing data in binary form. - dudley.Ondricka commented on September 19th 19 at 12:16
: I wrote it - Missouri.Hoeg commented on September 19th 19 at 12:19
I too.))) - dudley.Ondricka commented on September 19th 19 at 12:22
It is logical to assume that the extra characters from another place. They can easily be caught by the debugger. - Missouri.Hoeg commented on September 19th 19 at 12:25
September 19th 19 at 12:12
fopen('file.txt', 'wb') # where b suggests that we write the byte code
Use the flag 'ab', but nothing changes. Does the encoding of the php file? - dudley.Ondricka commented on September 19th 19 at 12:15
September 19th 19 at 12:14
the server comes line as if to open in a hex editor and look at the dump

That is what you are recording. Option b, of course, works, but no one will be for you to convert a hex representation back to bytes.
Read the string two characters, turn them into bytes and write it to a file.
Under the dump, I mean a text representation of bytes that is usually on the right of the hexadecimal view. - dudley.Ondricka commented on September 19th 19 at 12:17
If you take a string and write it to a file opened with the b key, then the file is exactly what was on the line. If you have to file something - sort it out, as it turned out in this line. No other methods of recording and dance with encodings will not help here. - dudley.Ondricka commented on September 19th 19 at 12:20

Find more questions by tags PHP