1

In C# (ASP.NET MVC) I have method which opens one file and put all the file content into the byte array like this:

byte[] bytes = File.ReadAllBytes(filename);

On PHP side I have PHP Slim Framework API method which is called from C# and I do a POST JSON object that contains this file byte array -> it has these properties:

  • myObjectId - int - id of my object
  • filename - string - file name
  • fileBytes - array - byte array of this file

You can get the demo of this JSON content here: https://justpaste.it/1d8jq

On PHP side I do json_decode of what I received like this:

$fileObject = json_decode($app->request->getBody());

And I get the same array of bytes in member:

$fileBytes = $fileObject->fileBytes;

The question now is how I can save this byte array into the Word file (it is a Word file). I used this method and it saves but it is some very strange content/strange symbols inside (not the original content)...

$binStr = '';
foreach ($fileBytes as $fileByte) 
{
  $binStr .= pack('i', $fileByte);          
}
$binStr .= "\n";
$fileTemp = '/tmp/'.$filename;
file_put_contents($fileTemp, $binStr);

So, how I can properly save these bytes (this byte array has the same elements/integers in PHP like in C# - looks like it is correctly sent))?

If this matters, C# part is on Windows Server and PHP is on Linux/CentOS server.

1 Answer 1

3

Seems like you have a simple array of bytes as integers, so instead of using pack, you could just use chr. With some array_map magic, this could be written as this.

$fileObject = json_decode($app->request->getBody());
$data = implode(array_map('chr', $fileObject->fileBytes));
file_put_contents('/tmp/' . $fileObject->filename, $data);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.