PHP fseek() 相当于变量?

发布于 2024-07-25 00:45:19 字数 372 浏览 3 评论 0原文

我需要的是 PHP 的 fseek() 函数的等效函数。 该函数适用于文件,但我有一个包含二进制数据的变量,我想对其进行处理。 我知道我可以使用 substr() ,但这会很蹩脚 - 它用于字符串,而不是二进制数据。 另外,创建一个文件然后使用 fseek() 也不是我想要的。

也许是用流构建的东西?

编辑:好的,我快到了:

$data = fopen('data://application/binary;binary,'.$bin,'rb');

警告:无法打开流:rfc2397:非法参数

What I need is an equivalent for PHP's fseek() function. The function works on files, but I have a variable that contains binary data and I want to work on it. I know I could use substr(), but that would be lame - it's used for strings, not for binary data. Also, creating a file and then using fseek() is not what I am looking for either.

Maybe something constructed with streams?

EDIT: Okay, I'm almost there:

$data = fopen('data://application/binary;binary,'.$bin,'rb');

Warning: failed to open stream: rfc2397: illegal parameter

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

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

发布评论

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

评论(3

ㄟ。诗瑗 2024-08-01 00:45:20

我猜测,但也许所要求的是一种通过使用指针访问变量中的字节的方法..(像在c中一样使用字节数组 - 没有放置数据的内存开销在 php 数组中)并能够就地编辑它们,而无需复制数据的开销。

无法做到这一点是一个大问题,但如果操作系统在临时文件上使用 fseek 很好地缓存磁盘数据可能是一个解决方法。

I'm guessing, but maybe what is being asked for is a way to access bytes in a variable by using a pointer.. (using it like an array of bytes like you could do in c - without the memory overhead of putting the data in php arrays) and being able to edit them inplace without the overhead of copying the data.

Not being able to do this is a BIG problem, but if the operating system caches disk data well using fseek on a temporary file could be a workaround.

暖风昔人 2024-08-01 00:45:19

Kai:

你在这里差不多已经回答了自己。 流就是答案。 以下手动条目将具有启发性: http://us.php.net/manual /en/wrappers.data.php

它本质上允许您将任意数据传递给 PHP 的文件处理函数,例如 fopen(以及 fseek)。

然后你可以做类似的事情:

<?php

$data = fopen('data://mime/type;encoding,' . $binaryData);

fseek($data, 128);
?>

Kai:

You have almost answered yourself here. Streams are the answer. The following manual entry will be enlightening: http://us.php.net/manual/en/wrappers.data.php

It essentially allows you to pass arbitrary data to PHP's file handling functions such as fopen (and thus fseek).

Then you could do something like:

<?php

$data = fopen('data://mime/type;encoding,' . $binaryData);

fseek($data, 128);
?>
白首有我共你 2024-08-01 00:45:19

对变量中的数据进行 fseek 没有意义。 fseek 只是将文件句柄定位到指定的偏移量,因此下一个 fread 调用从该偏移量开始读取。 对于字符串来说,没有与 fread 相当的东西。

substr() 有什么问题吗?

对于文件,您可以执行以下操作:

$f = fopen(...)
fseek($f, offset)
$x = fread($f, len)

使用 substr:

$x = substr($var, offset, len)

fseek on data in a variable doesn't make sense. fseek just positions the file handle to the specified offset, so the next fread call starts reading from that offset. There is no equivalent of fread for strings.

Whats wrong with substr()?

With a file you would do:

$f = fopen(...)
fseek($f, offset)
$x = fread($f, len)

with substr:

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