0
<?php

// // $bin = pack("S", 65535);
// // $ray = unpack("S", $bin);
// $ray = pack("H*hex", $data);
// print_r($ray);die();
// echo "UNSIGNED SHORT VAL = ", $ray[1], "\n";


$file = file_get_contents("C:\\Users\\qwerty\Downloads\\image001.jpg", true);
// $file = file_get_contents("C:\\Users\\qwerty\\Downloads\\request.pdf", true);
$data = '0x'.unpack('H*hex', $file)['hex'];

header('Content-Description: File Transfer');
header("Content-Transfer-Encoding: Binary");
// header('Content-Disposition: attachment; filename="request.pdf');
header('Content-Disposition: attachment; filename="image001.jpg');
header('Cache-Control: must-revalidate');
header('Content-Length: 80685');
echo $data;
?>

I want am trying to create File from unpacking the same file. I am recreating this from my other function that unpacks a file stores the data in database and recreate the file again using the data. The above is my sample demo for the bigger function. What it does is unpack file content then echo again. Problem is the downloaded file (image or pdf) cant be opened. Any idea is appreciated

I think I saw the problem when I opened the file in notepad it has a newline before the image data how can I make sure there will be no new line in before the data?

9
  • Why are you trying to use unpack()? Why not just echo $file? Databases store in binary format. There shouldn't be a need to involve another method pack/unpack, etc... Commented Feb 8, 2019 at 1:30
  • Im storing the data in database. This is just a simple demo for the project im working on @FrankerZ Commented Feb 8, 2019 at 1:30
  • You should use blob columns or store file names in db. If you use packing try var_dump(pack('H*',(unpack("H*hex", $data))['hex'])); Commented Feb 8, 2019 at 1:38
  • @Quasimodo'sclone i have varbinary as column type in database. Let me try this suggestion of yours Commented Feb 8, 2019 at 1:43
  • The previous example should just output $data (unpacked, i.e. transformed to hex, and packed back to binary). However, I would store the file as is with a prepared statement. stackoverflow.com/questions/1747894/… Commented Feb 8, 2019 at 3:48

0

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.