Search
 
SCRIPT & CODE EXAMPLE
 

PHP

WordPress Image/Files uploads

In this file having solution how to fetch image using wp media in wordpress.
Comment

how to upload file in wordpress

    
    
class WLSM_Helper {

    public static function get_attachment_mime() {
		return array('image/jpg', 'image/jpeg', 'image/png', 'application/pdf', 'application/msword', 'application/vnd.openxmlformats-officedocument.wordprocessingml.document', 'application/vnd.ms-excel', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', 'application/vnd.ms-powerpoint', 'application/vnd.openxmlformats-officedocument.presentationml.presentation', 'application/x-rar-compressed', 'application/octet-stream', 'application/zip', 'application/octet-stream', 'application/x-zip-compressed', 'multipart/x-zip', 'video/x-flv', 'video/mp4', 'application/x-mpegURL', 'video/MP2T', 'video/3gpp', 'video/quicktime', 'video/x-msvideo', 'video/x-ms-wmv');
	}

	public static function is_valid_file( $file, $type = 'attachment' ) {
		$get_mime = 'get_' . $type . '_mime';

		if ( extension_loaded( 'fileinfo' ) ) {
			$finfo = finfo_open( FILEINFO_MIME_TYPE );
			$mime  = finfo_file( $finfo, $file['tmp_name'] );
			finfo_close( $finfo );

		} else {
			$mime = $file['type'];
		}

		if ( ! in_array( $mime, self::$get_mime() ) ) {
			return false;
		}

		return true;
	}
}


# use helper class to organize your code

$attachment = (isset($_FILES['attachment']) && is_array($_FILES['attachment'])) ? $_FILES['attachment'] : NULL;

if (isset($attachment['tmp_name']) && !empty($attachment['tmp_name'])) {
    if (!WLSM_Helper::is_valid_file($attachment, 'attachment')) {
        $errors['attachment'] = esc_html__('This file type is not allowed.', 'school-management');
    }
}

$attachment = media_handle_upload('attachment', 0);
if (is_wp_error($attachment)) {
    throw new Exception($attachment->get_error_message());
}
$data['attachment'] = $attachment;
Comment

PREVIOUS NEXT
Code Example
Php :: php slots 
Php :: php radian to cosine 
Php :: wordpress html classes 
Php :: wordpress page template comment 
Php :: laravel remove public from url htaccess 
Php :: radio button in php form 
Php :: breaks the collection into multiple smaller collections Laravel 
Php :: how to show arraylist in comma separated with last and in php 
Php :: php header accept post request from same domain 
Php :: laravel how to address to git repo for develop packages 
Php :: phpexcel set data type 
Php :: eventon php code 
Php :: laravel request 
Php :: make a global php function in laravel so that accessed anywhere 
Php :: No match for argument: phpmyadmin yum 
Php :: sorting table row data with php 
Php :: wc php coupon applied message still after coupon delete 
Php :: lastPage does not exist. 
Php :: paygate logout session on callback laravel 
Php :: how can i get input id in laravel 8 
Php :: small echo php 
Php :: PHP vsprintf — Return a formatted string 
Php :: buddy group hide notice join 
Php :: getIP php 
Php :: how to convert php code to html 
Php :: wordpress auto save draft 
Php :: withCount laravel assign generic name 
Php :: smarty shorthand if 
Php :: how to get count of rows in a table in laravel query 
Php :: php display result from html 
ADD CONTENT
Topic
Content
Source link
Name
6+2 =