[英]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 编辑
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.