Does php file() require fclose() at the en

2019-09-04 16:16发布

Does PHP automatically close the file after a file(); function, or does it require fclose(); or similar?

1条回答
劳资没心,怎么记你
2楼-- · 2019-09-04 17:05

Does PHP automatically close the file after a file(); function, or does it require fclose(); or similar?

No, file() doesn't require a fclose() call. You can see that in the source code of the function that it all ends nice and clean, so you don't have to call fclose() or do anything similar, you simple can call file().

Source code:

/* {{{ proto array file(string filename [, int flags[, resource context]])
   Read entire file into an array */
PHP_FUNCTION(file)
{
    char *filename;
    size_t filename_len;
    char *p, *s, *e;
    register int i = 0;
    char eol_marker = '\n';
    zend_long flags = 0;
    zend_bool use_include_path;
    zend_bool include_new_line;
    zend_bool skip_blank_lines;
    php_stream *stream;
    zval *zcontext = NULL;
    php_stream_context *context = NULL;
    zend_string *target_buf;

    /* Parse arguments */
    if (zend_parse_parameters(ZEND_NUM_ARGS(), "p|lr!", &filename, &filename_len, &flags, &zcontext) == FAILURE) {
        return;
    }
    if (flags < 0 || flags > (PHP_FILE_USE_INCLUDE_PATH | PHP_FILE_IGNORE_NEW_LINES | PHP_FILE_SKIP_EMPTY_LINES | PHP_FILE_NO_DEFAULT_CONTEXT)) {
        php_error_docref(NULL, E_WARNING, "'" ZEND_LONG_FMT "' flag is not supported", flags);
        RETURN_FALSE;
    }

    use_include_path = flags & PHP_FILE_USE_INCLUDE_PATH;
    include_new_line = !(flags & PHP_FILE_IGNORE_NEW_LINES);
    skip_blank_lines = flags & PHP_FILE_SKIP_EMPTY_LINES;

    context = php_stream_context_from_zval(zcontext, flags & PHP_FILE_NO_DEFAULT_CONTEXT);

    stream = php_stream_open_wrapper_ex(filename, "rb", (use_include_path ? USE_PATH : 0) | REPORT_ERRORS, NULL, context);
    if (!stream) {
        RETURN_FALSE;
    }

    /* Initialize return array */
    array_init(return_value);

    if ((target_buf = php_stream_copy_to_mem(stream, PHP_STREAM_COPY_ALL, 0)) != NULL) {
        s = target_buf->val;
        e = target_buf->val + target_buf->len;

        if (!(p = (char*)php_stream_locate_eol(stream, target_buf))) {
            p = e;
            goto parse_eol;
        }

        if (stream->flags & PHP_STREAM_FLAG_EOL_MAC) {
            eol_marker = '\r';
        }

        /* for performance reasons the code is duplicated, so that the if (include_new_line)
         * will not need to be done for every single line in the file. */
        if (include_new_line) {
            do {
                p++;
parse_eol:
                add_index_stringl(return_value, i++, s, p-s);
                s = p;
            } while ((p = memchr(p, eol_marker, (e-p))));
        } else {
            do {
                int windows_eol = 0;
                if (p != target_buf->val && eol_marker == '\n' && *(p - 1) == '\r') {
                    windows_eol++;
                }
                if (skip_blank_lines && !(p-s-windows_eol)) {
                    s = ++p;
                    continue;
                }
                add_index_stringl(return_value, i++, s, p-s-windows_eol);
                s = ++p;
            } while ((p = memchr(p, eol_marker, (e-p))));
        }

        /* handle any left overs of files without new lines */
        if (s != e) {
            p = e;
            goto parse_eol;
        }
    }

    if (target_buf) {
        zend_string_free(target_buf);
    }
    php_stream_close(stream);
}
/* }}} */

See at the end 3rd line from the bottom it ends the function nice and clean:

php_stream_close(stream);
查看更多
登录 后发表回答