There is no problem not worth solving, and no technology not worth learning!

Generate a stream resource based on Fopen in PHP 7.4, operate the disk, and adjust it to a memory-based implementation

fopen 的参数 $filename 支持协议,参考:https://www.php.net/manual/zh/wrappers.php.php 。基于 php://memory 实现,避免操作磁盘。php://memory 和 php://temp 是一个类似文件 包装器的数据流,允许读写临时数据。 两者的唯一区别是 php://memory 总是把数据储存在内存中, 而 php://temp 会在内存量达到预定义的限制后(默认是 2MB)存入临时文件中

1. In PHP 7.4, a stream resource is generated based on Fopen, and the disk is operated. The code is implemented as follows.


$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. Print the result, as shown in Figure 1

打印结果
Figure 1

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 — create a temporary file.


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

exit;


4. Print the result, as shown in Figure 2

打印结果
Figure 2

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, search: C:\users\lenovo\appdata\local\temp\the7a4d.tmp, which exists. Search: C:\Users\Lenovo\AppData\Local\Temp\php14b.tmp, which has been automatically deleted. as shown in Figure 3

两者主要的差别在于,在脚本结束后,搜索:C:\Users\Lenovo\AppData\Local\Temp\the7A4D.tmp,其是存在的。搜索:C:\Users\Lenovo\AppData\Local\Temp\php14B.tmp,其已经被自动删除
Figure 3

6. The parameter $filename of fopen supports the protocol, refer to:https://www.php.net/manual/zh/wrappers.php.php. Avoid manipulating disks based on php://memory implementation. php://memory and php://temp are a stream-like data stream that allows read and write temporary data. The only difference between the two is that php://memory always stores the data in memory, and php://temp will be stored in the temporary file after the memory amount reaches the predefined limit (default is 2MB). as shown in Figure 4

fopen 的参数 $filename 支持协议,参考:https://www.php.net/manual/zh/wrappers.php.php 。基于 php://memory 实现,避免操作磁盘。php://memory 和 php://temp 是一个类似文件 包装器的数据流,允许读写临时数据。 两者的唯一区别是 php://memory 总是把数据储存在内存中, 而 php://temp 会在内存量达到预定义的限制后(默认是 2MB)存入临时文件中
Figure 4

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

exit;



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)



Need long-term technical maintenance or remote troubleshooting?

I am a PHP / Go backend engineer with 15+ years of experience, focused on existing system maintenance, bug fixing, performance optimization, server troubleshooting, WordPress maintenance, and small feature iterations.

If your project is facing any of the following issues, we can start with a small troubleshooting task first:

  • ✅ PHP / Laravel / Yii2 legacy systems without active maintenance
  • ✅ Go / Gin backend APIs that need troubleshooting or optimization
  • ✅ Slow, broken, or unstable WordPress websites
  • ✅ Nginx / MySQL / Redis / Linux server issues
  • ✅ CDN / Cloudflare / DNS / HTTPS configuration problems
  • ✅ Long-term remote technical support or part-time maintenance

More details: About Me & Collaboration

WeChat: 13980074657
Email: shuijingwanwq@gmail.com
Telegram: @shuijingwan
GitHub: https://github.com/shuijingwan

评论

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.