PHP fseek() 相当于变量?
我需要的是 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我猜测,但也许所要求的是一种通过使用指针访问变量中的字节的方法..(像在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.
Kai:
你在这里差不多已经回答了自己。 流就是答案。 以下手动条目将具有启发性: http://us.php.net/manual /en/wrappers.data.php
它本质上允许您将任意数据传递给 PHP 的文件处理函数,例如 fopen(以及 fseek)。
然后你可以做类似的事情:
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:
对变量中的数据进行 fseek 没有意义。 fseek 只是将文件句柄定位到指定的偏移量,因此下一个 fread 调用从该偏移量开始读取。 对于字符串来说,没有与 fread 相当的东西。
substr() 有什么问题吗?
对于文件,您可以执行以下操作:
使用 substr:
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:
with substr: