Page 1 of 1

Using fopen(), fread(), fseek() for data in memory instead?

Posted: Sat Dec 13, 2008 12:08 pm
by kaisellgren
Hi,

I have a class that uses fseek(), fread(), fopen() to read from a file. The thing is, I have the data in the memory in a variable instead of in a file. Don't please ask for question why or provide alternatives, what I need to get done is to make these functions to work with the data in the variable. Got any ideas?

Re: Using fopen(), fread(), fseek() for data in memory instead?

Posted: Sat Dec 13, 2008 12:16 pm
by Eran
Is it binary data? what's preventing you from using regular string functions to operate on it?

Re: Using fopen(), fread(), fseek() for data in memory instead?

Posted: Sat Dec 13, 2008 12:46 pm
by kaisellgren
pytrin wrote:Is it binary data? what's preventing you from using regular string functions to operate on it?
Those file functions are somewhat 800 times called. And it becomes real hard to convert all fseeks with its third parameters... it's a big class...

It's binary data yes.

Re: Using fopen(), fread(), fseek() for data in memory instead?

Posted: Sat Dec 13, 2008 1:16 pm
by Eran
Well the simplest approach would be to write that data into a file and process it from there.

If you're concerned about performance, you might consider mapping those fread(), fseek() etc to a class method with the same interface and replacing the calls in the code to those methods. You can then check if the handle provided is a file resource or a string and process accordingly.

Re: Using fopen(), fread(), fseek() for data in memory instead?

Posted: Sat Dec 13, 2008 1:48 pm
by mmj
On a project I was recently working on I had to upload POST data to an FTP server, and since the ftp_[f]put functions only support files I used tmpfile(), which did the job quite nicely.

Re: Using fopen(), fread(), fseek() for data in memory instead?

Posted: Sat Dec 13, 2008 1:57 pm
by kaisellgren
pytrin wrote:Well the simplest approach would be to write that data into a file and process it from there.

If you're concerned about performance, you might consider mapping those fread(), fseek() etc to a class method with the same interface and replacing the calls in the code to those methods. You can then check if the handle provided is a file resource or a string and process accordingly.
Thanks for the help.

I can't really make the file, since the server removes it (too big = 5mb)... that's why I basically need it to keep it in mem and process from there...

Re: Using fopen(), fread(), fseek() for data in memory instead?

Posted: Sat Dec 13, 2008 2:04 pm
by requinix
fopen and all those functions work on files. Not variables. There's nothing you can do about that.

Using memory mapping or something like that just sounds stubborn.

Re: Using fopen(), fread(), fseek() for data in memory instead?

Posted: Sat Dec 13, 2008 2:04 pm
by Eran
You can try a memory output wrapper. Never tried it myself but should work:

Code: Select all

$handle = fopen("php://memory",'w+');
fwrite($handle,$filecontents);

Re: Using fopen(), fread(), fseek() for data in memory instead?

Posted: Sat Dec 13, 2008 2:09 pm
by kaisellgren
pytrin wrote:You can try a memory output wrapper. Never tried it myself but should work:

Code: Select all

$handle = fopen("php://memory",'w+');
fwrite($handle,$filecontents);
Great!

Does that php://memory always work ? May it be disabled in some servers?

Re: Using fopen(), fread(), fseek() for data in memory instead?

Posted: Sat Dec 13, 2008 2:21 pm
by Eran
I don't think it can be disabled per se, however it is only available since PHP 5.1.0. Also, memory limits on PHP script execution could be problematic as well (although those are usually set in the 16Mb-32Mb range on most servers).

Re: Using fopen(), fread(), fseek() for data in memory instead?

Posted: Sat Dec 13, 2008 2:44 pm
by kaisellgren
pytrin wrote:I don't think it can be disabled per se, however it is only available since PHP 5.1.0. Also, memory limits on PHP script execution could be problematic as well (although those are usually set in the 16Mb-32Mb range on most servers).
I'm able to use 5.1.2 and I'm trying it on 8 MB mem limit, no problems :)

Thanks for the php://memory, works sweet.