WC_Download_Handler::readfile_from_handle │ private static │ WC 1.0

Read an already-open file handle in chunks and echo its content.

Method of the class: WC_Download_Handler{}

No Hooks.

Returns

string. One of the READ_RESULT_* constants.

Usage

$result = WC_Download_Handler::readfile_from_handle( $handle, $start, $length );
$handle(resource) (required)
Open file handle, e.g. from fopen().
$start(int)
Byte offset/position of the beginning from which to read from the file.
$length(int)
Length of the chunk to be read from the file in bytes, 0 means until the end of file.

WC_Download_Handler::readfile_from_handle() code WC 11.2.2

private static function readfile_from_handle( $handle, $start = 0, $length = 0 ) {
	if ( ! defined( 'WC_CHUNK_SIZE' ) ) {
		define( 'WC_CHUNK_SIZE', 1024 * 1024 );
	}

	$read_length = (int) WC_CHUNK_SIZE;
	$output_sent = false;

	if ( $length ) {
		$end = $start + $length - 1;

		@fseek( $handle, $start ); // phpcs:ignore Generic.PHP.NoSilencedErrors.Discouraged
		$p = @ftell( $handle ); // phpcs:ignore Generic.PHP.NoSilencedErrors.Discouraged

		while ( ! @feof( $handle ) && $p <= $end ) { // phpcs:ignore Generic.PHP.NoSilencedErrors.Discouraged
			// Don't run past the end of file.
			if ( $p + $read_length > $end ) {
				$read_length = $end - $p + 1;
			}

			$chunk = @fread( $handle, $read_length ); // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged, WordPress.WP.AlternativeFunctions.file_system_operations_fread -- Streamed downloads require fread(); suppress warnings so they do not corrupt the binary response, and handle false below.

			if ( false === $chunk ) {
				@fclose( $handle ); // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged, WordPress.WP.AlternativeFunctions.file_system_operations_fclose -- The read has already failed; suppress close warnings and report failure below.
				return $output_sent ? self::READ_RESULT_FAILURE_AFTER_OUTPUT : self::READ_RESULT_FAILURE_BEFORE_OUTPUT;
			}

			echo $chunk; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Download chunks are raw binary data and must not be HTML-escaped.
			$output_sent = $output_sent || '' !== $chunk;
			$p = @ftell( $handle ); // phpcs:ignore Generic.PHP.NoSilencedErrors.Discouraged

			if ( ob_get_length() ) {
				ob_flush();
				flush();
			}
		}
	} else {
		while ( ! @feof( $handle ) ) { // @codingStandardsIgnoreLine.
			$chunk = @fread( $handle, $read_length ); // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged, WordPress.WP.AlternativeFunctions.file_system_operations_fread -- Streamed downloads require fread(); suppress warnings so they do not corrupt the binary response, and handle false below.

			if ( false === $chunk ) {
				@fclose( $handle ); // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged, WordPress.WP.AlternativeFunctions.file_system_operations_fclose -- The read has already failed; suppress close warnings and report failure below.
				return $output_sent ? self::READ_RESULT_FAILURE_AFTER_OUTPUT : self::READ_RESULT_FAILURE_BEFORE_OUTPUT;
			}

			echo $chunk; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Download chunks are raw binary data and must not be HTML-escaped.
			$output_sent = $output_sent || '' !== $chunk;
			if ( ob_get_length() ) {
				ob_flush();
				flush();
			}
		}
	}

	// phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged, WordPress.WP.AlternativeFunctions.file_system_operations_fclose -- Suppress close warnings so they do not corrupt the binary response, and report failure below.
	if ( @fclose( $handle ) ) {
		return self::READ_RESULT_SUCCESS;
	}

	return $output_sent ? self::READ_RESULT_FAILURE_AFTER_OUTPUT : self::READ_RESULT_FAILURE_BEFORE_OUTPUT;
}