Using fopen(), fread(), fseek() for data in memory instead?
Moderator: General Moderators
- 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?
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?
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?
Is it binary data? what's preventing you from using regular string functions to operate on it?
- 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?
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...pytrin wrote:Is it binary data? what's preventing you from using regular string functions to operate on it?
It's binary data yes.
Re: Using fopen(), fread(), fseek() for data in memory instead?
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.
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?
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.
- 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?
Thanks for the help.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.
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?
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.
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.
Re: Using fopen(), fread(), fseek() for data in memory instead?
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);- 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?
Great!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);
Does that php://memory always work ? May it be disabled in some servers?
Re: Using fopen(), fread(), fseek() for data in memory instead?
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).
- 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?
I'm able to use 5.1.2 and I'm trying it on 8 MB mem limit, no problemspytrin 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).
Thanks for the php://memory, works sweet.