dqe55175 2018-11-25 02:20
浏览 39

Javascript到PHP文件上传丢失的字节

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?

  • 写回答

0条回答

    报告相同问题?

    悬赏问题

    • ¥20 神经网络Sequential name=sequential, built=False
    • ¥16 Qphython 用xlrd读取excel报错
    • ¥15 单片机学习顺序问题!!
    • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
    • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
    • ¥15 相敏解调 matlab
    • ¥15 求lingo代码和思路
    • ¥15 公交车和无人机协同运输
    • ¥15 stm32代码移植没反应
    • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效