Intercepting upload

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
danc81
Forum Newbie
Posts: 16
Joined: Mon May 11, 2009 2:00 pm

Intercepting upload

Post by danc81 »

Hi,

Is it possible in PHP to intercept an upload like you can in ASP? So if I wanted to perform some operations on the data as it came in, can I do it in PHP or is there another way to do it perhaps? I'm running PHP 5.2.6 and Apache 2.2.3.

Thanks!
User avatar
Darhazer
DevNet Resident
Posts: 1011
Joined: Thu May 14, 2009 3:00 pm
Location: HellCity, Bulgaria

Re: Intercepting upload

Post by Darhazer »

danc81
Forum Newbie
Posts: 16
Joined: Mon May 11, 2009 2:00 pm

Re: Intercepting upload

Post by danc81 »

Thanks but that doesn't really say much about how to access the data during the upload. I want to be able to run some additional processing on the data as it comes in rather than waiting until the upload has finished.
User avatar
Darhazer
DevNet Resident
Posts: 1011
Joined: Thu May 14, 2009 3:00 pm
Location: HellCity, Bulgaria

Re: Intercepting upload

Post by Darhazer »

You can get the temporary filename, and the current size, so you will cache how many bytes were processed already, you will read from the file starting at the last position and processing to the current end position...

Well, it depends what you want to do exactly.
You can for example read from the php://input stream, which is the raw post data. This won't work with a mutlipart/form-data. And I think that your PHP script will be started when all data is received by the server, so php://input will contain the whole file.
Another solution is to write a daemon in PHP, that listen on a socket...

So, the big question is what is the goal of this processing?
danc81
Forum Newbie
Posts: 16
Joined: Mon May 11, 2009 2:00 pm

Re: Intercepting upload

Post by danc81 »

The initial goal is to CRC the input to verify it uploaded OK, there are some other features I would like to add later but the goal at the moment is a simple CRC32 as it uploads
danc81
Forum Newbie
Posts: 16
Joined: Mon May 11, 2009 2:00 pm

Re: Intercepting upload

Post by danc81 »

My POST is not multipart/formdata so the php://input may be a possible solution.
danc81
Forum Newbie
Posts: 16
Joined: Mon May 11, 2009 2:00 pm

Re: Intercepting upload

Post by danc81 »

I've been trying out php://input and have run into an issue where if I upload a large file I get a "PHP fatal error allowed memory size of .... has been exhausted, tried to allocate 123456789 bytes", where "123456789" is the size of my file. I thought using php://input this would not allocate the whole size and let me read as it came in but that doesn't seem to be the case. Is there something I can do to allow me to read the data as it comes in instead of allocating the whole lot first?

Thanks
User avatar
Darhazer
DevNet Resident
Posts: 1011
Joined: Thu May 14, 2009 3:00 pm
Location: HellCity, Bulgaria

Re: Intercepting upload

Post by Darhazer »

It depends how you are reading the data.
Use fopen() / fgets() will read data line by line, or fread() - byte by byte (or as many bytes you want)
If you are doing file_get_contents you will read the whole request and this is not what you want to do...
danc81
Forum Newbie
Posts: 16
Joined: Mon May 11, 2009 2:00 pm

Re: Intercepting upload

Post by danc81 »

I am using fopen / fread to read the data and I have no proof that it is reading the whole contents into memory but I get the "out of memory error at unknown line 0" before any of my logging at the start of the php file so I can only think that it is reading it all into memory?

Out of interest, is the PHP memory limit per script or global?
Post Reply