doukang7486 2017-12-27 19:01
浏览 171

内存对PHP中通过HTTP PUT发送二进制文件的影响,没有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. 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).

The usual (or popular, I guess) way of uploading files would be using multipart/form-data encoding, which would be handled automatically by PHP. In that case, PHP would write the contents in a temporary file, which I believe exists in a storage device rather than memory.

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.

My question is:

0) Does PHP store the full request body in memory (at least once), regardless of the enctype it gets?

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?

1a) Does php's copy-on-write mechanism help me here? 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 ?

PS: I do not need to send anything else (e.g., metadata) with the file, nor do I have the need to send multiple files in a single request.

PPS: Sorry if my post is incomprehensible, I'd be happy to provide other details if anything seems confusing or incomplete.

EDIT

  • Added Question 0.
  • Slightly Modified questions in hopes of making it less confusing.
  • 写回答

1条回答 默认 最新

  • dqm4675 2017-12-27 19:15
    关注

    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?

    Yeah, tons. With file_put_contents() alone, you need the full data in-memory before writing it. It's meant for more of a utility for small files and such.

    1a) Does php's copy-on-write mechanism help me here? 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 ?

    Yes! Check out the php://input stream. This will allow you to access the raw POST body.

    http://php.net/manual/en/wrappers.php.php#wrappers.php.input

    评论

报告相同问题?

悬赏问题

  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 正弦信号发生器串并联电路电阻无法保持同步怎么办
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 个人网站被恶意大量访问,怎么办
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)