创建假文件句柄

发布于 2024-12-29 15:05:14 字数 342 浏览 1 评论 0原文

这可能是不可能的,但我想我应该四处打听,看看我是否错过了什么。我正在寻找一种使用需要文件指针句柄的函数而不需要编写实际文件的方法。

我知道我可以使用临时文件,执行操作,然后将文件读回输出缓冲区并删除该文件。然而,这意味着从写入磁盘到从磁盘读取的延迟。就我而言,如果我可以做类似的事情,那么速度和资源会好得多,

$tmp = createFakeFile();
fputscsv($tmp, array(/*** an array of csv fields ***/));
echo $tmp;

我怀疑这是否存在,但如果您曾经听说过做这样的事情的方法,我很想听听,

谢谢

This is probably impossible but i thought i would ask around and see if i missed something. What im looking for is a way of using functions that require file pointer handle without the need to write an actual file.

I know i could use a temporary file, do my operations then read the file back into the output buffer and delete the file. However this means latency from writing to disk then reading from the disk. It would be far better for speed and resources in my case if i could just do something like

$tmp = createFakeFile();
fputscsv($tmp, array(/*** an array of csv fields ***/));
echo $tmp;

I doubt this exists but if you've ever heard of a way of doing something like this i'd love to hear about it

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

豆芽 2025-01-05 15:05:15

您可以为此使用 php://memory 和 php://temp 包装文件。

引用:

php://memory 和 php://temp 是读写流,允许
要存储在类似文件的包装器中的临时数据。唯一的
两者之间的区别在于 php://memory 将始终存储其
内存中的数据,而 php://temp 将使用临时文件
存储的数据量达到预定义的限制(默认为 2 MB)。
该临时文件的位置的确定方式与
sys_get_temp_dir() 函数。

可以通过附加来控制 php://temp 的内存限制
/maxmemory:NN,其中 NN 是要保留的最大数据量
使用临时文件之前的内存,以字节为单位。

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

You can use php://memory and php://temp wrapper files for this.

Cite:

php://memory and php://temp are read-write streams that allow
temporary data to be stored in a file-like wrapper. The only
difference between the two is that php://memory will always store its
data in memory, whereas php://temp will use a temporary file once the
amount of data stored hits a predefined limit (the default is 2 MB).
The location of this temporary file is determined in the same way as
the sys_get_temp_dir() function.

The memory limit of php://temp can be controlled by appending
/maxmemory:NN, where NN is the maximum amount of data to keep in
memory before using a temporary file, in bytes.

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

吻风 2025-01-05 15:05:15

确实有一种方法可以做到这一点: stream_wrapper_register()

这个示例类应该做你想做的事情,它创建实际上只是保存在内存中的文件句柄。

话虽如此, php://memory 在某种程度上,php://temp

There is indeed a way to do it: stream_wrapper_register().

This example class should do what you want, it creates a file handle that is actually just held in memory.

Having said that, so does php://memory and to some degree, php://temp.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文