没有不值得去解决的问题,也没有不值得去学习的技术!

Generate a Stream Resource With fopen in PHP 7.4: Adjust Disk Operations to a Fully Memory-Based Implementation

The $filename parameter of fopen supports protocols. Reference: https://www.php.net/manual/zh/wrappers.php.php . Implementing it based on php://memory avoids disk operations. php://memory and php://temp are file-like wrapper streams that allow reading and writing temporary data. The only difference between the two is that php://memory always stores data in memory, whereas php://temp stores data in a temporary file once the memory usage reaches a predefined limit (default is 2 MB)

1. In PHP 7.4, generating a stream resource based on fopen operates on the disk. The code implementation is as follows.

PHP

$tmp = tempnam(sys_get_temp_dir(), 'theme_asset_cdn_zip_stream');
$stream = fopen($tmp, 'w+');
var_dump(stream_get_meta_data($stream), $stream);

exit;

2. The printed result is shown in Figure 1.

Printed result
Figure 1
Plaintext

array(9) {
  ["timed_out"]=>
  bool(false)
  ["blocked"]=>
  bool(true)
  ["eof"]=>
  bool(false)
  ["wrapper_type"]=>
  string(9) "plainfile"
  ["stream_type"]=>
  string(5) "STDIO"
  ["mode"]=>
  string(2) "w+"
  ["unread_bytes"]=>
  int(0)
  ["seekable"]=>
  bool(true)
  ["uri"]=>
  string(46) "C:\Users\Lenovo\AppData\Local\Temp\the7A4D.tmp"
}
resource(2041) of type (stream)


3. Reference: https://www.php.net/manual/zh/resource.php , list of resource types. Use tmpfile — creates a temporary file.

PHP

$stream = tmpfile();
var_dump(stream_get_meta_data($stream), $stream);

exit;

4. The printed result is shown in Figure 2.

Printed result
Figure 2
Plaintext

array(9) {
  ["timed_out"]=>
  bool(false)
  ["blocked"]=>
  bool(true)
  ["eof"]=>
  bool(false)
  ["wrapper_type"]=>
  string(9) "plainfile"
  ["stream_type"]=>
  string(5) "STDIO"
  ["mode"]=>
  string(3) "r+b"
  ["unread_bytes"]=>
  int(0)
  ["seekable"]=>
  bool(true)
  ["uri"]=>
  string(45) "C:\Users\Lenovo\AppData\Local\Temp\php14B.tmp"
}
resource(2041) of type (stream)


5. The main difference between the two is that after the script ends, searching for C:\Users\Lenovo\AppData\Local\Temp\the7A4D.tmp shows that it still exists. Searching for C:\Users\Lenovo\AppData\Local\Temp\php14B.tmp shows that it has been automatically deleted. As shown in Figure 3.

The main difference between the two is that after the script ends, searching for C:\Users\Lenovo\AppData\Local\Temp\the7A4D.tmp shows that it still exists. Searching for C:\Users\Lenovo\AppData\Local\Temp\php14B.tmp shows that it has been automatically deleted
Figure 3

6. The $filename parameter of fopen supports protocols. Reference: https://www.php.net/manual/zh/wrappers.php.php . Implementing it based on php://memory avoids disk operations. php://memory and php://temp are file-like wrapper streams that allow reading and writing temporary data. The only difference between the two is that php://memory always stores data in memory, whereas php://temp stores data in a temporary file once the memory usage reaches a predefined limit (default is 2 MB). As shown in Figure 4.

The $filename parameter of fopen supports protocols. Reference: https://www.php.net/manual/zh/wrappers.php.php . Implementing it based on php://memory avoids disk operations. php://memory and php://temp are file-like wrapper streams that allow reading and writing temporary data. The only difference between the two is that php://memory always stores data in memory, whereas php://temp stores data in a temporary file once the memory usage reaches a predefined limit (default is 2 MB)
Figure 4
PHP

$stream = fopen('php://memory', 'w+');
var_dump(stream_get_meta_data($stream), $stream);

exit;

Plaintext

array(9) {
  ["timed_out"]=>
  bool(false)
  ["blocked"]=>
  bool(true)
  ["eof"]=>
  bool(false)
  ["wrapper_type"]=>
  string(3) "PHP"
  ["stream_type"]=>
  string(6) "MEMORY"
  ["mode"]=>
  string(3) "w+b"
  ["unread_bytes"]=>
  int(0)
  ["seekable"]=>
  bool(true)
  ["uri"]=>
  string(12) "php://memory"
}
resource(2041) of type (stream)


关于作者 · 持续学习与技术实践

我是一名拥有 15 年以上开发经验的技术从业者,自 2013 年起持续运营个人技术博客,记录工作与个人项目中遇到的真实问题,以及解决问题过程中的探索、实践和思考。

除了技术写作,我也在持续开发和维护自己的独立项目,探索新技术和新工具在实际场景中的应用。我始终相信,没有不值得去解决的问题,也没有不值得去学习的技术。

如果这篇文章对你有所帮助,欢迎浏览博客中的其他文章,也欢迎交流技术经验与想法。

关于我  ·  GitHub  ·  邮件联系