返回介绍

wp_check_filetype_and_ext()

发布于 2017-09-11 11:41:15 字数 5777 浏览 1017 评论 0 收藏 0

wp_check_filetype_and_ext( string $file,  string $filename,  array $mimes = null )

Attempt to determine the real file type of a file.


description

If unable to, the file name extension will be used to determine type.

If it’s determined that the extension does not match the file’s real type, then the "proper_filename" value will be set with a proper filename and extension.

Currently this function only supports renaming images validated via wp_get_image_mime().


参数

$file

(string) (Required) Full path to the file.

$filename

(string) (Required) The name of the file (may differ from $file due to $file being in a tmp directory).

$mimes

(array) (Optional) Key is the file extension with value as the mime type.

Default value: null


返回值

(array) Values for the extension, MIME, and either a corrected filename or false if original $filename is valid.


源代码

File: wp-includes/functions.php

function wp_check_filetype_and_ext( $file, $filename, $mimes = null ) {
	$proper_filename = false;

	// Do basic extension validation and MIME mapping
	$wp_filetype = wp_check_filetype( $filename, $mimes );
	$ext = $wp_filetype['ext'];
	$type = $wp_filetype['type'];

	// We can't do any further validation without a file to work with
	if ( ! file_exists( $file ) ) {
		return compact( 'ext', 'type', 'proper_filename' );
	}

	$real_mime = false;

	// Validate image types.
	if ( $type && 0 === strpos( $type, 'image/' ) ) {

		// Attempt to figure out what type of image it actually is
		$real_mime = wp_get_image_mime( $file );

		if ( $real_mime && $real_mime != $type ) {
			/**
			 * Filters the list mapping image mime types to their respective extensions.
			 *
			 * @since 3.0.0
			 *
			 * @param  array $mime_to_ext Array of image mime types and their matching extensions.
			 */
			$mime_to_ext = apply_filters( 'getimagesize_mimes_to_exts', array(
				'image/jpeg' => 'jpg',
				'image/png'  => 'png',
				'image/gif'  => 'gif',
				'image/bmp'  => 'bmp',
				'image/tiff' => 'tif',
			) );

			// Replace whatever is after the last period in the filename with the correct extension
			if ( ! empty( $mime_to_ext[ $real_mime ] ) ) {
				$filename_parts = explode( '.', $filename );
				array_pop( $filename_parts );
				$filename_parts[] = $mime_to_ext[ $real_mime ];
				$new_filename = implode( '.', $filename_parts );

				if ( $new_filename != $filename ) {
					$proper_filename = $new_filename; // Mark that it changed
				}
				// Redefine the extension / MIME
				$wp_filetype = wp_check_filetype( $new_filename, $mimes );
				$ext = $wp_filetype['ext'];
				$type = $wp_filetype['type'];
			} else {
				// Reset $real_mime and try validating again.
				$real_mime = false;
			}
		}
	}

	// Validate files that didn't get validated during previous checks.
	if ( $type && ! $real_mime && extension_loaded( 'fileinfo' ) ) {
		$finfo = finfo_open( FILEINFO_MIME_TYPE );
		$real_mime = finfo_file( $finfo, $file );
		finfo_close( $finfo );

		/*
		 * If $real_mime doesn't match what we're expecting, we need to do some extra
		 * vetting of application mime types to make sure this type of file is allowed.
		 * Other mime types are assumed to be safe, but should be considered unverified.
		 */
		if ( $real_mime && ( $real_mime !== $type ) && ( 0 === strpos( $real_mime, 'application' ) ) ) {
			$allowed = get_allowed_mime_types();

			if ( ! in_array( $real_mime, $allowed ) ) {
				$type = $ext = false;
			}
		}
	}

	/**
	 * Filters the "real" file type of the given file.
	 *
	 * @since 3.0.0
	 *
	 * @param array  $wp_check_filetype_and_ext File data array containing 'ext', 'type', and
	 *                                          'proper_filename' keys.
	 * @param string $file                      Full path to the file.
	 * @param string $filename                  The name of the file (may differ from $file due to
	 *                                          $file being in a tmp directory).
	 * @param array  $mimes                     Key is the file extension with value as the mime type.
	 */
	return apply_filters( 'wp_check_filetype_and_ext', compact( 'ext', 'type', 'proper_filename' ), $file, $filename, $mimes );
}

更新日志

Versiondescription
3.0.0Introduced.

相关函数

Uses

  • wp-includes/functions.php: wp_get_image_mime()
  • wp-includes/functions.php: wp_check_filetype()
  • wp-includes/functions.php: get_allowed_mime_types()
  • wp-includes/functions.php: getimagesize_mimes_to_exts
  • wp-includes/functions.php: wp_check_filetype_and_ext
  • wp-includes/plugin.php: apply_filters()
  • Show 1 more use Hide more uses

Used By

  • wp-admin/includes/file.php: _wp_handle_upload()
  • wp-admin/includes/ajax-actions.php: wp_ajax_upload_attachment()
  • wp-admin/custom-header.php: Custom_Image_Header::step_2_manage_upload()
  • wp-admin/custom-background.php: Custom_Background::handle_upload()

User Contributed Notes

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文