How to save byte array in PHP file?

Asked

Viewed 794 times

2

I have a array of bytes which is an image that is saved in BD, which method to use to save on hard drive?

1 answer

3


You need package the array and record. Well roughly speaking since you have not given details of what you want, nor demonstrated what you have already done you can do so:

foreach ($imagem as $byte) {
    $binario .= pack("C", $byte); 
}
$arquivo = fopen("arquivo.bin", 'wb');
fwrite($arquivo, $binario);
fclose($arquivo);

I put in the Github for future reference.

The exact way may vary according to your need.

If the format is already in binary can only do the recording:

$arquivo = fopen("arquivo.bin", 'wb');
fwrite($arquivo, $imagem);
fclose($arquivo);
  • Actually, I need to create a thumbnail of that image that’s in the comic book. First I researched on the internet some classes that resize this image, I found the canvas class made by a Brazilian, I want to use it, but this class does not le array of bytes, ie, first I want to save the photo in the folder, and then I upload the photo with the canvas class resize it, and saved again in the same location.

  • 2

    @But why didn’t you say that in the question? You asked something else! I suggest you post a separate question asking how to generate a thumbnail from an image in the bank.

  • Why generate I believe I can, but I will rephrase in a new question in more detail, others can go through it. Thank you. But first I’ll test the mustache solution

  • @bigown the foreach did not recognize the byte array showing following message Invalid argument supplied for foreach()

  • You didn’t post anything you did. Now you’re saying it didn’t work, but I don’t know what you did. It’s hard to help like this. You have to give information. Most likely what you went through is even a array as you said.

  • I managed to save, it was not necessary the foreach

  • So it’s already in binary format.

  • should be this I picked up from the BD and thought it came in byte array isn’t it?. Thanks @bigown

  • That may be, but since you didn’t go into detail, I had to assume.

  • But anyway this solution worked thanks.

Show 5 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.