WC_Download_Handler::download_file_xsendfile()public staticWC 1.0

Download a file using X-Sendfile, X-Lighttpd-Sendfile, or X-Accel-Redirect if available.

Method of the class: WC_Download_Handler{}

Return

null. Nothing (null).

Usage

$result = WC_Download_Handler::download_file_xsendfile( $file_path, $filename );
$file_path(string) (required)
File path.
$filename(string) (required)
File name.

WC_Download_Handler::download_file_xsendfile() code WC 9.4.2

public static function download_file_xsendfile( $file_path, $filename ) {
	$parsed_file_path = self::parse_file_path( $file_path );

	/**
	 * Fallback on force download method for remote files. This is because:
	 * 1. xsendfile needs proxy configuration to work for remote files, which cannot be assumed to be available on most hosts.
	 * 2. Force download method is more secure than redirect method if `allow_url_fopen` is enabled in `php.ini`.
	 */
	if ( $parsed_file_path['remote_file'] && ! apply_filters( 'woocommerce_use_xsendfile_for_remote', false ) ) {
		do_action( 'woocommerce_download_file_force', $file_path, $filename );
		return;
	}

	if ( function_exists( 'apache_get_modules' ) && in_array( 'mod_xsendfile', apache_get_modules(), true ) ) {
		self::download_headers( $parsed_file_path['file_path'], $filename );
		$filepath = apply_filters( 'woocommerce_download_file_xsendfile_file_path', $parsed_file_path['file_path'], $file_path, $filename, $parsed_file_path );
		header( 'X-Sendfile: ' . $filepath );
		exit;
	} elseif ( stristr( getenv( 'SERVER_SOFTWARE' ), 'lighttpd' ) ) {
		self::download_headers( $parsed_file_path['file_path'], $filename );
		$filepath = apply_filters( 'woocommerce_download_file_xsendfile_lighttpd_file_path', $parsed_file_path['file_path'], $file_path, $filename, $parsed_file_path );
		header( 'X-Lighttpd-Sendfile: ' . $filepath );
		exit;
	} elseif ( stristr( getenv( 'SERVER_SOFTWARE' ), 'nginx' ) || stristr( getenv( 'SERVER_SOFTWARE' ), 'cherokee' ) ) {
		self::download_headers( $parsed_file_path['file_path'], $filename );
		$xsendfile_path = trim( preg_replace( '`^' . str_replace( '\\', '/', getcwd() ) . '`', '', $parsed_file_path['file_path'] ), '/' );
		$xsendfile_path = apply_filters( 'woocommerce_download_file_xsendfile_x_accel_redirect_file_path', $xsendfile_path, $file_path, $filename, $parsed_file_path );
		header( "X-Accel-Redirect: /$xsendfile_path" );
		exit;
	}

	// Fallback.
	wc_get_logger()->warning(
		sprintf(
			/* translators: %1$s contains the filepath of the digital asset. */
			__( '%1$s could not be served using the X-Accel-Redirect/X-Sendfile method. A Force Download will be used instead.', 'woocommerce' ),
			$file_path
		)
	);
	self::download_file_force( $file_path, $filename );
}