Switch language

wpseek.com
A WordPress-centric search engine for devs and theme authors




wp_check_filetype_and_ext [ WordPress Function ]

wp_check_filetype_and_ext ( $file, $filename, $mimes = null )
Parameters:
  • (string) $file Full path to the image.
  • (string) $filename The filename of the image (may differ from $file due to $file being in a tmp directory)
  • (array) $mimes Optional. Key is the file extension with value as the mime type.
Returns:
  • (array) Values for the extension, MIME, and either a corrected filename or false if original $filename is valid
Defined at:



Attempt to determine the real file type of a file.

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 validating images known to getimagesize().

Source


<?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 );
    
extract$wp_filetype );

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

    
// We're able to validate images using GD
    
if ( $type && === strpos$type'image/' ) && function_exists('getimagesize') ) {

        
// Attempt to figure out what type of image it actually is
        
$imgstats = @getimagesize$file );

        
// If getimagesize() knows what kind of image it really is and if the real MIME doesn't match the claimed MIME
        
if ( !empty($imgstats['mime']) && $imgstats['mime'] != $type ) {
            
// This is a simplified array of MIMEs that getimagesize() can detect and their extensions
            // You shouldn't need to use this filter, but it's here just in case
            
$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$imgstats['mime'] ] ) ) {
                
$filename_parts explode'.'$filename );
                
array_pop$filename_parts );
                
$filename_parts[] = $mime_to_ext$imgstats['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 );
                
extract$wp_filetype );
            }
        }
    }

    
// Let plugins try and validate other types of files
    // Should return an array in the style of array( 'ext' => $ext, 'type' => $type, 'proper_filename' => $proper_filename )
    
return apply_filters'wp_check_filetype_and_ext'compact'ext''type''proper_filename' ), $file$filename$mimes );
}
?>

Examples [ wp-snippets.com ]

Google Arama Sonuçlarý

Dahasý ...

Kullanýcý Tartýþmalarý [ wordpress.org ]

- Bulunamadý -

Yeni bir konu yaz ...

0 User Note(s)

Henüz yok. Ýlk sen ol!

Yeni Ekle ...



HTML5 Powered with CSS3 / Styling, Performance & Integration, and Semantics