简体   繁体   English

不带多部分/表单数据的PHP中通过HTTP PUT发送二进制文件的内存影响

[英]Memory Impact on Sending Binary File through HTTP PUT in PHP without multipart/form-data

I recently wrote a code where I send a normal PUT request which has contents of a binary file in the request body with the Content-Type header set to the mime type of said file. 我最近写了一个代码,在其中发送一个普通的PUT请求,该请求在请求正文中具有二进制文件的Content-Type ,并且Content-Type标头设置为所述文件的mime类型。 In the server, I handle that by just reading the raw request body in a variable, employing some validation: size (using strlen ) and mime types (using finfo->buffer ) and then copying the contents to a file (using file_put_contents ). 在服务器中,我只需要读取变量中的原始请求主体,然后进行一些验证即可:大小(使用strlen )和mime类型(使用finfo->buffer ),然后将内容复制到文件中(使用file_put_contents )。

The usual (or popular, I guess) way of uploading files would be using multipart/form-data encoding, which would be handled automatically by PHP. 上载文件的通常方式(或流行的方式)是使用multipart/form-data编码,这将由PHP自动处理。 In that case, PHP would write the contents in a temporary file, which I believe exists in a storage device rather than memory. 在那种情况下,PHP会将内容写入临时文件中,我认为该文件存在于存储设备中而不是内存中。

Now, I went through the code in https://github.com/php/php-src/blob/master/main/rfc1867.c , and it seemed to put the request body in memory anyways. 现在,我遍历了https://github.com/php/php-src/blob/master/main/rfc1867.c中的代码,它似乎仍然将请求正文存储在内存中。

My question is: 我的问题是:

0) Does PHP store the full request body in memory (at least once), regardless of the enctype it gets? 0)PHP是否将完整的请求主体存储在内存中(至少一次),而不管它得到的enctype什么?

1) Does getting all the raw body and writing it to a file (using file_put_contents ) uses more memory compared to using php's internal multipart/form-data handling mechanism to upload a file? 1)与使用php内部的multipart/form-data处理机制上传文件相比,获取所有原始内容并将其写入文件(使用file_put_contents )是否会占用更多内存?

1a) Does php's copy-on-write mechanism help me here? 1a)php的写时复制机制对我有帮助吗? that is to say am I allocating new blocks of memory when I store the raw body content in some variable (and not a file)? 也就是说,当我将原始内容存储在某个变量(而不是文件)中时,是否分配新的内存块? Or am I just pointing to the request data that is already on the memory (if there is) ? 还是我只是指向内存中已经存在的请求数据(如果有)?

2) If what I'm doing does impact memory, do you have any suggestions for improvement that does not involve using multipart/form-data ? 2)如果我的工作确实影响了内存,您是否有不涉及使用multipart/form-data改进建议?

PS: I do not need to send anything else (eg, metadata) with the file, nor do I have the need to send multiple files in a single request. PS:我不需要与文件一起发送任何其他内容(例如,元数据),也不需要在单个请求中发送多个文件。

PPS: Sorry if my post is incomprehensible, I'd be happy to provide other details if anything seems confusing or incomplete. PPS:对不起,如果我的帖子令人难以理解,如果有任何令人困惑或不完整的地方,我很乐意提供其他详细信息。

EDIT 编辑

  • Added Question 0. 添加了问题0。
  • Slightly Modified questions in hopes of making it less confusing. 稍作修改的问题,希望可以减少混乱。

1) Does getting all the raw body and writing it to a file (using file_put_contents makes me use more memory rather than letting PHP handle it? 1)是否获取所有原始主体并将其写入文件(使用file_put_contents使我使用更多的内存,而不是让PHP处理它?

Yeah, tons. 是的,吨 With file_put_contents() alone, you need the full data in-memory before writing it. 仅使用file_put_contents() ,在写入数据之前就需要完整的内存数据。 It's meant for more of a utility for small files and such. 它意味着更多的小文件之类的实用程序。

1a) Does php's copy-on-write mechanism help me here? 1a)php的写时复制机制对我有帮助吗? that is to say am I allocating new blocks of memory when I store the raw body content in some variable (and not a file)? 也就是说,当我将原始内容存储在某个变量(而不是文件)中时,是否分配新的内存块?

I'm not familiar with specifically what you're referring to, but yes if you store the body content in a variable, a copy of it is sitting in memory. 我对所指的内容并不熟悉,但是可以,如果将正文内容存储在变量中,则将其副本存储在内存中。

2) If what I'm doing does impact memory, do you have any suggestions for improvement that does not involve using multipart/form-data ? 2)如果我的工作确实影响了内存,您是否有不涉及使用multipart / form-data的改进建议?

Yes! 是! Check out the php://input stream. 查看php://input流。 This will allow you to access the raw POST body. 这将允许您访问原始POST正文。

http://php.net/manual/en/wrappers.php.php#wrappers.php.input http://php.net/manual/zh-CN/wrappers.php.php#wrappers.php.input

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 通过php发布文件“ multipart / form-data” - posting file “multipart/form-data” through php 尝试通过执行PUT请求的API上传模型时文件丢失-多部分/表单数据请求-Laravel / PHP - File is missing when trying to upload a model through API doing a PUT request - Multipart/form-data Requests - Laravel / PHP 如何将HTTP PUT请求中的multipart / form-data解析为PHP中的关联数组? - How can I parse multipart/form-data from an HTTP PUT request into an associative array in PHP? 将.net应用程序中的multipart / form-data发送到php服务器 - Sending multipart/form-data from .net app to php server 使用curl put上传文件 - multipart / form-data - Upload file using curl put - multipart/form-data PATCH / PUT不接受多部分/表格数据文件上传? - PATCH/PUT not accepting multipart/form-data file uploads? PHP POST数据未通过enctype =“ multipart / form-data”传递 - PHP POST data not coming through with enctype=“multipart/form-data” 如何使用 HTTP POST multipart/form-data 将文件上传到服务器? - How to upload file to server with HTTP POST multipart/form-data? 在没有用户干预的情况下发布多部分/表单数据 - POSTing a multipart/form-data without user intervention in PHP 离子Android发送多部分/表单数据 - Ion Android sending multipart/form-data
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM