WordPress.org

Make WordPress Core

Opened 5 years ago

Closed 5 years ago

#32561 closed defect (bug) (wontfix)

Warning: fread() expects parameter 1 to be resource, boolean given in get_file_data()

Reported by: SpYdR Owned by:
Milestone: Priority: normal
Severity: normal Version: 4.1.3
Component: General Keywords: close
Focuses: Cc:

Description

PHP throw this error when wordpress try to read metadata from a file which is missing or isn't readable (throwing a "failed to open stream: No such file or directory" warning).

In wp-includes/functions.php the get_file_data function code must be :

// We don't need to write to the file, so just open for reading.
$fp = fopen( $file, 'r' );
if ($fp === false) {
	return array();
}

// Pull only the first 8kiB of the file in.
$file_data = fread( $fp, 8192 );

// PHP will close file handle, but we are good citizens.
fclose( $fp );

// Make sure we catch CR-only line endings.
$file_data = str_replace( "\r", "\n", $file_data );

/**
 * Filter extra file headers by context.
 *
 * The dynamic portion of the hook name, `$context`, refers to
 * the context where extra headers might be loaded.
 *
 * @since 2.9.0
 *
 * @param array $extra_context_headers Empty array by default.
 */
if ( $context && $extra_headers = apply_filters( "extra_{$context}_headers", array() ) ) {
	$extra_headers = array_combine( $extra_headers, $extra_headers ); // keys equal values
	$all_headers = array_merge( $extra_headers, (array) $default_headers );
} else {
	$all_headers = $default_headers;
}

foreach ( $all_headers as $field => $regex ) {
	if ( preg_match( '/^[ \t\/*#@]*' . preg_quote( $regex, '/' ) . ':(.*)$/mi', $file_data, $match ) && $match[1] )
		$all_headers[ $field ] = _cleanup_header_comment( $match[1] );
	else
		$all_headers[ $field ] = '';
}

return $all_headers;

Change History (3)

#1 @dd32
5 years ago

I'm tempted to suggest that get_file_data() should only ever be called on a file that actually exists, and that the error raised here is good to alert developers to the fact they've called it on a non-existant file

#2 @dd32
5 years ago

  • Keywords close added

Actually, further to this, the error is actually several:

Warning: fopen(/file/doesnt/exist): failed to open stream: No such file or directory in /src/wp-includes/functions.php on line 4258
Warning: fread() expects parameter 1 to be resource, boolean given in /src/wp-includes/functions.php on line 4261
Warning: fclose() expects parameter 1 to be resource, boolean given in /src/wp-includes/functions.php on line 4264

we could prefix it with a file_exists() or is_readable(), but i still hold that the calling function should run that.

#3 @johnbillion
5 years ago

  • Milestone Awaiting Review deleted
  • Resolution set to wontfix
  • Status changed from new to closed

Agreed with dd32. This is expected behaviour.

Note: See TracTickets for help on using tickets.