I need to upload files from javascript to php for binary reading and it seems to be adding an extra 0 byte at the beginning and skipping one at the end.
Javascript Code:
let formData = new FormData();
formData.append("files[]", fileInput.files[0]);
uploadButton.classList.add("is-loading");
fetch(url, {
method: "POST",
body: formData
}).then(response => {
if(response.status === 200) showContent(fileInput.files[0].name);
});
PHP Code:
if(isset($_FILES["files"])) {
$name = $_FILES["files"]["name"][0];
$tmp = $_FILES["files"]["tmp_name"][0];
$path = "";
do { $newName = str_replace(".uexp", "", $name) . "-" . generateRandomString(); $path = "../filestore/$newName.uexp"; } while(file_exists($path));
move_uploaded_file($tmp, $path);
$str = file_get_contents($path);
$byteArray = unpack("C*", $str);
$_SESSION["Name"] = $name;
$_SESSION["Path"] = $path;
$_SESSION["File"] = serialize($byteArray);
}
When I read out the file in the filestore directory (so after move_uploaded_file) it is different than the one originally uploaded.
The file that was originally uploaded starts with (in hex): EA 00 00 and ends with (in hex): 83 2A 9E. The file that can be found in the filestore folder starts with (in hex): 00 EA 00 and ends with (in hex): 83 2A.
As a result, the file can no longer be read by the original application. I'm using PHP 7.2 and IIS as my platform.
Anything I'm missing?