Skip to content

php://temp does not preserve file-position when switched to temporary file #8333

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from

Conversation

bitbaendiger
Copy link
Contributor

This is my first PR for PHP. Please forgive me if I violated some rules that I didn't know so far or understood wrong while reading your guidelines.

I discovered a small flaw in PHP's php://temp stream-wrapper:
The stream is kept in memory until a given amount of data was written to it. Once this limit is reached during fwrite() PHP will offload the stream to a temporary file.

PHP detects whether the limit is reached by adding the length to be written to the length of the actual stream. This is right as long as the file-position is at the end of the stream. If the pointer is somewhere else, there might be no need to switch to a temporary file at all. Plus: The file-position is not preserved during the switch so contents will be written to the end of the file regardless of the previous file-position.

I wrote a very small test for this bug that may explain the issue better than my words. I also included a fix for the issue itself.

Thank you in advance!

@cmb69 cmb69 closed this in 84c18f9 Apr 11, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants