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!
Intercepting upload
Moderator: General Moderators
Re: Intercepting upload
What's new in PHP V5.2, Part 5: Tracking file upload progress
Hope this will help you!
Hope this will help you!
Re: Intercepting upload
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.
Re: Intercepting upload
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?
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?
Re: Intercepting upload
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
Re: Intercepting upload
My POST is not multipart/formdata so the php://input may be a possible solution.
Re: Intercepting upload
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
Thanks
Re: Intercepting upload
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...
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...
Re: Intercepting upload
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?
Out of interest, is the PHP memory limit per script or global?