With PHP and Apache, is it possible to start the PHP script after the headers are received, but before the body is sent ?
I mean kind of this:
PUT /sync/testStream.php HTTP/1.1
Host: localhost
User-Agent: test
Content-Length: 500
Content-Type: text/plain
<start the script here>
hello
this is a test
The objective is to lock a file during the script (during its upload actually).
I tried to read from php://input, with no success: the script is started only when the whole body is received.
Here is my script:
<?php
echo "Hello. Start sending data.";
ob_flush();
$file = fopen('php://input', 'r');
if ($file === false) {
die("Could not open php://input");
}
while (($line = fgets($file)) !== false) {
echo $line;
}
echo "Bye";
Any hint is welcome !
I have seen this question on here before (can’t find it right now, but I have) and after much discussion/debate, that conclusion was that No, you can’t.
This is because the file upload process and receiving of the body of the request is handled by the overlying web server, and PHP is not fired up until after the request has been fully received/parsed/validated.
One point here is that your script appears to be attempting to establish full-duplex communication over HTTP, which is not possible. HTTP is by it’s very nature a half-duplex protocol – it has a request-response architecture and you cannot start sending a response before you have completely received the request, it would be a protocol violation.
If you explain exactly what you are trying to do/why you want to lock a file during upload, maybe we can find an alternative solution.