如何接收使用“application/octet-stream”发送的 POST 数据在 PHP 中?

发布于 2024-10-06 11:02:35 字数 362 浏览 0 评论 0原文

这就是我正在处理的事情。我们的一个计划有一个支持表格,用户可以使用它来请求支持。此表单的作用是,它向 PHP 脚本执行 HTTP POST 请求,该脚本应该收集信息并将其转发到支持电子邮件地址。

POST 请求包含三个 Content-Type: text/plain 类型的文本字段,可以使用 $_POST['fieldname'] 在 PHP 中轻松读取。然而,此 POST 请求中的某些内容是 Content-Type: application/octet-stream 类型的文件。使用 $_POST 似乎不适用于这些文件。我该如何读取这些文件的内容?

先感谢您。

Here's what I'm dealing with. One of our programs has a support form that users use to request support. What this form does is, it performs an HTTP POST request to a PHP script that's supposed to collect the info and forward it to the support e-mail address.

The POST request contains three text fields of the type Content-Type: text/plain which can be easily read in PHP using $_POST['fieldname']. Some of the content in this POST request, however, are files, of type Content-Type: application/octet-stream. Using $_POST doesn't seem to work with these files. How do I go about reading the contents of these files?

Thank you in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

恰似旧人归 2024-10-13 11:02:35

您必须使用 $_FILES 超级全局:

$contents = file_get_contents($_FILES['myfile']['tmp_name']);

您可以在手册中找到更多信息。仅当您在请求中使用multipart/form-data编码时,这才有效。

您还可以读取原始 POST 数据,然后自行解析:

$rawPost = file_get_contents('php://input');

You have to use the $_FILES superglobal:

$contents = file_get_contents($_FILES['myfile']['tmp_name']);

You can find more information in the manual. This will only work if you are using multipart/form-data encoding in your request.

You can otherwise read the raw POST data, then parse it yourself:

$rawPost = file_get_contents('php://input');
作死小能手 2024-10-13 11:02:35

这些属于 $_FILES 超全局。在这里阅读:http://php.net/manual/en/features。 file-upload.php

基本上,您得到的是一个类似于 POST 的数组,但包含文件大小、临时名称和文件的临时位置等内容,您可以使用它来测试文件并将文件移动到永久位置地点。

PHP 手册中的更多一般信息: http://php.net/manual/en /features.file-upload.php

Those fall under the $_FILES superglobal. Read about it here: http://php.net/manual/en/features.file-upload.php

Basically, what you get is an array similar to POST, but with things like file size, temp name and the temporary location of the file which you can use to test and move the file to a permanent location.

Further general info from the PHP manual: http://php.net/manual/en/features.file-upload.php

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文