I would like to comment something I found, I’m not sure if I missed some steps, or, is something already known. I haven’t found info about this subject.
I’m currently migrating from Slim v3 into v4 using slim/psr7 to create the requests. Before now I was using the getSize function in the PSR7 StreamInterface to measure the requests size, but, when changed to the new version it didn’t work as before, I can’t get the request size from this function, it always return null. After digging a bit the code I have found that the base PHP resource on my requests has changed from previous versions, the library slim/psr7 creates a request with a php://input resource which doesn’t allow the fstat operation done in the Stream class of the same library, so, it can’t get the size from the resource. Before now it was php://temp w+ which allows the fstat function.
I just have tested this behavior also with other PSR-7 implementations, such as Nyholm and I get the same result (null). Nyholm also uses php://input within the ServerRequestCreator::fromGlobals method.
Indeed, it looks like a php://input resource / stream does not provide support for fstat. See here:
Thanks for your quick response, I have tried Guzzle implementation also, and it still uses the php://temp writable resource, Laminas uses php://input, it depends on their implementation.
By now, my intention is to continue with the Slim/Psr-7 implementation, I was thinking about using the Content-Length header, I suppose it will be mostly the same.
Sorry I can’t answer until now. It was my first thought, but, the request body is being read into memory, on large requests, depending on the usage it can duplicate the memory needed.
Thank you so much for the proposal, have a nice weekend!
Indeed, this is not the best solution, because it reads the request into memory.
Another, more memory efficient way would be to read the last position of the stream:
$size = $request->getBody()->tell();
I just found out, that when you send POST request as multipart/form-data the input stream length is always null, because PHP itself has already processed that stream to populate the $_POST variable. Just in this case, the request size would be null anyway.