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

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
kaisellgren
DevNet Resident
Posts: 1675
Joined: Sat Jan 07, 2006 5:52 am
Location: Lahti, Finland.

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

Post 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?
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

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

Post by Eran »

Is it binary data? what's preventing you from using regular string functions to operate on it?
User avatar
kaisellgren
DevNet Resident
Posts: 1675
Joined: Sat Jan 07, 2006 5:52 am
Location: Lahti, Finland.

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

Post 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.
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

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

Post 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.
mmj
Forum Contributor
Posts: 118
Joined: Fri Oct 31, 2008 4:00 pm

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

Post 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.
User avatar
kaisellgren
DevNet Resident
Posts: 1675
Joined: Sat Jan 07, 2006 5:52 am
Location: Lahti, Finland.

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

Post 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...
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

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

Post 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.
Last edited by requinix on Sat Dec 13, 2008 2:06 pm, edited 2 times in total.
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

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

Post 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);
User avatar
kaisellgren
DevNet Resident
Posts: 1675
Joined: Sat Jan 07, 2006 5:52 am
Location: Lahti, Finland.

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

Post 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?
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

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

Post 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).
User avatar
kaisellgren
DevNet Resident
Posts: 1675
Joined: Sat Jan 07, 2006 5:52 am
Location: Lahti, Finland.

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

Post 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.
Post Reply