Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8346513
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T07:02:38+00:00 2026-06-09T07:02:38+00:00

As per the documentation , php://input does not always contain the HTTP request body.

  • 0

As per the documentation, php://input does not always contain the HTTP request body.

I have some code that always opens the stream when the script starts, to make it available to the user application:

$stream = fopen('php://input', 'rb');

If the user application tries to read from an empty stream, when Content-Length > 0, I want my library to throw an exception:

The request body is not available.

My problem is that:

  • If the stream is empty, feof() returns false while fread() has not been called;
  • I can’t fread() 1 byte then fseek(), as this stream does not support seeking;
  • I can’t open another php://input and try to read from it beforehand, as we can’t rely on this stream to be available more than once;
  • I don’t want to blindly copy php://input to php://temp before giving the hand to the user application, for performance reasons.

Consequently, is it possible to tell whether php://input contains data right after fopen(), but before any fread()?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T07:02:39+00:00Added an answer on June 9, 2026 at 7:02 am

    The solution I found is to create a class representing the request body, and reading from this class instead of php://input directly:

    class RequestBody
    {
        protected $firstByte;
    
        protected $isEmpty;
    
        public function __construct($stream)
        {
            $this->firstByte = fread($stream, 1);
            $this->isEmpty = ($this->firstByte === '');
    
            // ...
        }
    
        public function isEmpty()
        {
            return $this->isEmpty;
        }
    
        public function read($length)
        {
            // ...
        }
    }
    
    $body = new RequestBody(fopen('php://input', 'rb'));
    

    The class constructor reads the first byte of the stream, storing it for later use, and knowing instantly whether the stream is empty.

    read() hides this complexity, by returning the first byte + $length - 1 bytes the first time it’s called.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Per a client's request I have written a communication class that inherits from webclient.
The PHP documentation states that php://input can only be read once. In my application
I have some code that requests some JSON from an API. When data is
So I followed the instructions per this link http://www.opencart.com/index.php?route=documentation/documentation&path=32_35_60 but my paypal account is
As per PHP documentation, mysql needs to be enabled with PHP. http://www.php.net/manual/en/mysql.installation.php PHP 5.0.x,
As per the redtube API Documentation, Method getVideoEmbedCode retrieves embed code (BASE64 encoded) for
As per http://www.w3schools.com/cssref/sel_last-child.asp When i am trying to apply :last-child for my code, the
How does one pre-populate a Formish form? The obvious method as per the documentation
I've installed PEAR onto my Windows 7 PC using PEAR, as per: http://www.propelorm.org/documentation/01-installation.html Now,
I am pulling my hair out. I have created some simple functions that generate

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.