1. Ruslan Osmanov
  2. pecl-event
  3. Issues
Issue #2 wontfix

Add support of php://temp

Ruslan Osmanov
repo owner created an issue

Add support of the php://temp stream. See https://bugs.php.net/bug.php?id=64652

Comments (2)

  1. Ruslan Osmanov reporter

    I doubt if this can be done easily / efficiently, because php://temp (which is a particular case of php://memory) has no particular file descriptor we can watch for. At least until the memory limit specified for the stream is not exceeded:

    static size_t php_stream_temp_write(php_stream *stream, const char *buf, size_t count TSRMLS_DC)
    {
        php_stream_temp_data *ts = (php_stream_temp_data*)stream->abstract;
        assert(ts != NULL);
    
        if (!ts->innerstream) {
            return -1;
        }
        if (php_stream_is(ts->innerstream, PHP_STREAM_IS_MEMORY)) {
            size_t memsize;
            char *membuf = php_stream_memory_get_buffer(ts->innerstream, &memsize);
    
            if (memsize + count >= ts->smax) {
                php_stream *file = php_stream_fopen_tmpfile();
                php_stream_write(file, membuf, memsize);
                php_stream_free_enclosed(ts->innerstream, PHP_STREAM_FREE_CLOSE);
                ts->innerstream = file;
                php_stream_encloses(stream, ts->innerstream);
            }
        }
        return php_stream_write(ts->innerstream, buf, count);
    }
    

    Well, we can make use of php_stream_temp_cast, which just makes a temporary file from the memory:

        /* perform the conversion and then pass the request on to the innerstream */
        membuf = php_stream_memory_get_buffer(ts->innerstream, &memsize);
        file = php_stream_fopen_tmpfile();
        php_stream_write(file, membuf, memsize);
        pos = php_stream_tell(ts->innerstream);
    
        php_stream_free_enclosed(ts->innerstream, PHP_STREAM_FREE_CLOSE);
        ts->innerstream = file;
        php_stream_encloses(stream, ts->innerstream);
        php_stream_seek(ts->innerstream, pos, SEEK_SET);
    
        return php_stream_cast(ts->innerstream, castas, ret, 1);
    

    But is it sane? I'd rather make a regular file on tmpfs.

  2. Ruslan Osmanov reporter

    I may be wrong, but I still think this feature redundant. AFAIK, we've no enough control over the memory. We can listen to events on the "swap" file, but obviously it's not reasonable as we could use a regular file for that.

  3. Log in to comment