wc_is_file_valid_csv()WC 3.6.5

Check if a CSV file is valid.

Return

true|false.

Usage

wc_is_file_valid_csv( $file, $check_path );
$file(string) (required)
File name.
$check_path(true|false)
If should check for the path.
Default: true

Changelog

Since 3.6.5 Introduced.

wc_is_file_valid_csv() code WC 8.7.0

function wc_is_file_valid_csv( $file, $check_path = true ) {
	/**
	 * Filter check for CSV file path.
	 *
	 * @since 3.6.4
	 * @param bool   $check_import_file_path If requires file path check. Defaults to true.
	 * @param string $file                   Path of the file to be checked.
	 */
	$check_import_file_path = apply_filters( 'woocommerce_csv_importer_check_import_file_path', true, $file );

	if ( $check_path && $check_import_file_path && false !== stripos( $file, '://' ) ) {
		return false;
	}

	/**
	 * Filter CSV valid file types.
	 *
	 * @since 3.6.5
	 * @param array $valid_filetypes List of valid file types.
	 */
	$valid_filetypes = apply_filters(
		'woocommerce_csv_import_valid_filetypes',
		array(
			'csv' => 'text/csv',
			'txt' => 'text/plain',
		)
	);

	$filetype = wp_check_filetype( $file, $valid_filetypes );

	if ( in_array( $filetype['type'], $valid_filetypes, true ) ) {
		return true;
	}

	return false;
}