php file() 最后需要 fclose() 吗?

Does php file() require fclose() at the end?

PHP 会在 file(); 函数后自动关闭文件,还是需要 fclose(); 或类似函数?

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

不,file() 不需要 fclose() 调用。您可以在函数的源代码中看到它一切都结束得很干净,所以您不必调用 fclose() 或做任何类似的事情,您可以简单地调用 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);
}
/* }}} */

看到从底部开始的第 3 行,它结束了函数,干净利落:

php_stream_close(stream);