1. In PHP 7.4, generating a stream resource based on fopen operates on the disk. The code implementation is 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. The printed result is shown in 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 — creates a temporary file.
$stream = tmpfile();
var_dump(stream_get_meta_data($stream), $stream);
exit;
4. The printed result is shown in 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, 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.

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.

$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)
