如何接收未作为 multipart/form 发送的上传文件? PHP

How to receive an uploaded file which is not sent as multipart/form? PHP

我从第三方那里得到了通过 http 协议向我发送文件的文档,我需要编写一个脚本来成功接收发送的文件。内容类型设置为 application/gzip 所以我无法使用 $_FILES 变量获取上传的文件,因为使用 multipart/form-data.

会很容易

这个link给了我一个提示:http://php.net/manual/en/features.file-upload.post-method.php

Note: Be sure your file upload form has attribute enctype="multipart/form-data" otherwise the file upload will not work.

我尝试使用下面的示例重现他们的 "client" 端来测试我的服务器 url http://blog.derakkilgo.com/2009/06/07/send-a-file-via-post-with-curl-and-php/

为了确保跨域发布可用,我使用了@slashingweapon 发布和解释的功能 CORS with php headers

必须有办法做到这一点 - 哈尔!

您好,我的理解是您只需要下载已上传到服务器的文件,尝试使用以下代码以二进制模式获取文件:

ftp_fget($conn_id, $file, FTP_BINARY, 0);

参考:http://php.net/manual/en/function.ftp-get.php

共享文件的第 3 方必须与您共享连接详细信息。

感谢@fusion3k 为我指明了正确的方向。

$rawData = file_get_contents("php://input") 提供了 RAW 数据,我必须从中解析文件。提取它很痛苦,因为它是一个二进制文件。我用了

  1. 以“\n”为分隔符展开数据$rawData = explode("\n", $rawData)
  2. 跳过前几行(对我来说是 3 行 header 数据)并将其余的放入 $valuableData
  3. 使用$convertedLine = unpack("H*", $valuableData[count($valuableData)-1])
  4. 将最后一行数据从字符串转换为二进制
  5. 从最后一行中删除最后一个字节的数据$convertedLine[1] = substr($convertedLine[1], 0, -2)(不知道为什么 `unpack 返回一个数组)
  6. $valuableData[count($valuableData)-1] = pack("H*", $convertedLine[1])
  7. implode("\n", $valuableData) 并将其写入文件