本文整理汇总了PHP中wp_check_filetype函数的典型用法代码示例。如果您正苦于以下问题:PHP wp_check_filetype函数的具体用法?PHP wp_check_filetype怎么用?PHP wp_check_filetype使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了wp_check_filetype函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: upload_file
/**
* @param strin $file_url
*
* @return int
*/
private function upload_file($file_url)
{
if (!filter_var($file_url, FILTER_VALIDATE_URL)) {
return false;
}
$contents = @file_get_contents($file_url);
if ($contents === false) {
return false;
}
$upload = wp_upload_bits(basename($file_url), null, $contents);
if (isset($upload['error']) && $upload['error']) {
return false;
}
$type = '';
if (!empty($upload['type'])) {
$type = $upload['type'];
} else {
$mime = wp_check_filetype($upload['file']);
if ($mime) {
$type = $mime['type'];
}
}
$attachment = array('post_title' => basename($upload['file']), 'post_content' => '', 'post_type' => 'attachment', 'post_mime_type' => $type, 'guid' => $upload['url']);
$id = wp_insert_attachment($attachment, $upload['file']);
wp_update_attachment_metadata($id, wp_generate_attachment_metadata($id, $upload['file']));
return $id;
}
示例2: add_image
static function add_image($image_url)
{
if (empty($image_url)) {
return FALSE;
}
// Add Featured Image to Post
$upload_dir = wp_upload_dir();
// Set upload folder
$image_data = file_get_contents($image_url);
// Get image data
$filename = basename($image_url);
// Create image file name
// Check folder permission and define file location
if (wp_mkdir_p($upload_dir['path'])) {
$file = $upload_dir['path'] . '/' . $filename;
} else {
$file = $upload_dir['basedir'] . '/' . $filename;
}
// Create the image file on the server
file_put_contents($file, $image_data);
// Check image file type
$wp_filetype = wp_check_filetype($filename, NULL);
// Set attachment data
$attachment = array('post_mime_type' => $wp_filetype['type'], 'post_title' => sanitize_file_name($filename), 'post_content' => '', 'post_status' => 'inherit');
// Create the attachment
$attach_id = wp_insert_attachment($attachment, $file);
// Include image.php
require_once ABSPATH . 'wp-admin/includes/image.php';
// Define attachment metadata
$attach_data = wp_generate_attachment_metadata($attach_id, $file);
// Assign metadata to attachment
wp_update_attachment_metadata($attach_id, $attach_data);
return $attach_id;
}
示例3: process_attachment
function process_attachment($post, $url)
{
// if the URL is absolute, but does not contain address, then upload it assuming base_site_url
//if ( preg_match( '|^/[\w\W]+$|', $url ) )
// $url = rtrim( $this->base_url, '/' ) . $url;
global $url_remap;
$upload = fetch_remote_file($url, $post);
if (is_wp_error($upload)) {
return $upload;
}
if ($info = wp_check_filetype($upload['file'])) {
$post['post_mime_type'] = $info['type'];
} else {
return new WP_Error('attachment_processing_error', __('Invalid file type', 'wordpress-importer'));
}
$post['guid'] = $upload['url'];
// as per wp-admin/includes/upload.php
$post_id = wp_insert_attachment($post, $upload['file']);
wp_update_attachment_metadata($post_id, wp_generate_attachment_metadata($post_id, $upload['file']));
// remap resized image URLs, works by stripping the extension and remapping the URL stub.
if (preg_match('!^image/!', $info['type'])) {
$parts = pathinfo($url);
$name = basename($parts['basename'], ".{$parts['extension']}");
// PATHINFO_FILENAME in PHP 5.2
$parts_new = pathinfo($upload['url']);
$name_new = basename($parts_new['basename'], ".{$parts_new['extension']}");
$url_remap[$parts['dirname'] . '/' . $name] = $parts_new['dirname'] . '/' . $name_new;
}
return $post_id;
}
示例4: wpua_avatar_upload
public function wpua_avatar_upload($file)
{
$filetype = wp_check_filetype($file->name);
$media_upload = array();
$media_upload['file'] = array('name' => $file->name, 'type' => $filetype['type'], 'tmp_name' => $file->path, 'error' => 0, 'size' => filesize($file->path));
$media_file = wp_handle_upload($media_upload['file'], array('test_form' => false, 'test_upload' => false, 'action' => 'custom_action'));
if ($media_file['file']) {
$url = $media_file['url'];
$filepath = $media_file['file'];
if ($image_meta = @wp_read_image_metadata($filepath)) {
if (trim($image_meta['title']) && !is_numeric(sanitize_title($image_meta['title']))) {
$title = $image_meta['title'];
}
}
$attachment = array('guid' => $url, 'post_mime_type' => $filetype['type'], 'post_title' => $title);
$attachment_id = wp_insert_attachment($attachment, $filepath);
if (!is_wp_error($attachment_id)) {
$this->delete_attachment_by_user($this->user_id);
wp_update_attachment_metadata($attachment_id, wp_generate_attachment_metadata($attachment_id, $filepath));
update_post_meta($attachment_id, '_wp_attachment_wp_user_avatar', $this->user_id);
$arr = wp_get_attachment_image_src($attachment_id, 'full');
$this->avatar_url = $arr[0];
$this->avatar_filename = basename($filepath);
$this->resource = $attachment_id;
$saved = $this->save();
if (!$saved) {
$this->delete_attachment($attachment_id);
return $saved;
}
return $saved;
}
} else {
return WP_Error('file_upload_problem', __("Media avatar could't uploading please check you have right permission for uploads folder.", 'wp-user-avatar-pro'));
}
}
示例5: actionSaveImage
public function actionSaveImage()
{
if (!empty($_POST['imageUrl'])) {
$url = \parse_url($_POST['imageUrl']);
if ($curlDescriptor = \curl_init($_POST['imageUrl'])) {
\curl_setopt($curlDescriptor, CURLOPT_HEADER, 0);
\curl_setopt($curlDescriptor, CURLOPT_RETURNTRANSFER, 1);
\curl_setopt($curlDescriptor, CURLOPT_BINARYTRANSFER, 1);
$rawImage = \curl_exec($curlDescriptor);
\curl_close($curlDescriptor);
if ($rawImage) {
include_once ABSPATH . 'wp-admin/includes/image.php';
include_once ABSPATH . 'wp-admin/includes/file.php';
include_once ABSPATH . 'wp-admin/includes/media.php';
$wpFileType = \wp_check_filetype(\basename($url['path']), null);
$tmpDir = \ini_get('upload_tmp_dir') ? \ini_get('upload_tmp_dir') : \sys_get_temp_dir();
$tempName = $tmpDir . '/' . \uniqid() . '.' . $wpFileType['ext'];
\file_put_contents($tempName, $rawImage);
$_FILES['async-upload'] = array('name' => \trim(\str_replace(' ', '', basename($tempName))), 'type' => $wpFileType['type'], 'tmp_name' => $tempName, 'error' => 0, 'size' => \filesize($tempName));
\media_handle_upload('async-upload', 0, array(), array('test_form' => false, 'action' => 'upload-attachment'));
\wp_send_json(array('status' => 'success'));
}
}
}
\wp_send_json(array('status' => 'error'));
}
示例6: simple_upload_csv_products_file
function simple_upload_csv_products_file()
{
$upload_feedback = '';
if (isset($_FILES['product_csv']) && $_FILES['product_csv']['size'] > 0) {
$arr_file_type = wp_check_filetype(basename($_FILES['product_csv']['name']));
$uploaded_file_type = $arr_file_type['ext'];
$allowed_file_type = 'csv';
if ($uploaded_file_type == $allowed_file_type) {
$wp_uploads_dir = wp_upload_dir();
$filepath = $wp_uploads_dir['basedir'] . '/simple-products.csv';
if (move_uploaded_file($_FILES['product_csv']['tmp_name'], $filepath)) {
simple_import_product_from_csv();
} else {
$upload_feedback = '<div class="al-box warning">' . __('There was a problem with your upload.', 'al-ecommerce-product-catalog') . '</div>';
}
} else {
$upload_feedback = '<div class="al-box warning">' . __('Please upload only CSV files.', 'al-ecommerce-product-catalog') . '</div>';
}
echo $upload_feedback;
} else {
$url = sample_import_file_url();
echo '<form method="POST" enctype="multipart/form-data"><input type="file" accept=".csv" name="product_csv" id="product_csv" /><input type="submit" class="button" value="' . __('Import Products', 'al-ecommerce-product-catalog') . '" /></form>';
echo '<div class="al-box info"><p>' . __("The CSV fields should be in following order: Image URL, Product Name, Product Price, Product Categories, Short Description, Long Description.", "al-ecommerce-product-catalog") . '</p><p>' . __("The first row should contain the field names. Semicolon should be used as the CSV separator.", "al-ecommerce-product-catalog") . '</p><a href="' . $url . '" class="button-primary">' . __('Download CSV Template', 'al-ecommerce-product-catalog') . '</a></div>';
}
}
示例7: dswoddil_site_icon_upload
/**
* Upload theme default site icon
*
* @return void
*/
function dswoddil_site_icon_upload()
{
$image_name = 'site-icon';
if (!function_exists('wp_update_attachment_metadata')) {
require_once ABSPATH . 'wp-admin/includes/image.php';
}
if (!dswoddil_get_attachment_by_post_name('dswoddil-' . $image_name)) {
$site_icon = get_template_directory() . '/img/' . $image_name . '.png';
// create $file array with the indexes show below
$file['name'] = $site_icon;
$file['type'] = 'image/png';
// get image size
$file['size'] = filesize($file['name']);
$file['tmp_name'] = $image_name . '.png';
$file['error'] = 1;
$file_content = file_get_contents($site_icon);
$upload_image = wp_upload_bits($file['tmp_name'], null, $file_content);
// Check the type of tile. We'll use this as the 'post_mime_type'.
$filetype = wp_check_filetype(basename($upload_image['file']), null);
$attachment = array('guid' => $upload_image['file'], 'post_mime_type' => $filetype['type'], 'post_title' => 'dswoddil-' . $image_name, 'post_content' => '', 'post_status' => 'inherit');
//insert wordpress attachment of uploaded image to get attachmen ID
$attachment_id = wp_insert_attachment($attachment, $upload_image['file']);
//generate attachment thumbnail
wp_update_attachment_metadata($attachment_id, wp_generate_attachment_metadata($attachment_id, $upload_image['file']));
add_post_meta($attachment_id, '_wp_attachment_context', $image_name);
}
}
示例8: import
public function import($attachment)
{
$saved_image = $this->_return_saved_image($attachment);
if ($saved_image) {
return $saved_image;
}
// Extract the file name and extension from the url
$filename = basename($attachment['url']);
if (function_exists('file_get_contents')) {
$options = ['http' => ['user_agent' => 'Mozilla/5.0 (X11; Ubuntu; Linux i686 on x86_64; rv:49.0) Gecko/20100101 Firefox/49.0']];
$context = stream_context_create($options);
$file_content = file_get_contents($attachment['url'], false, $context);
} else {
$file_content = wp_remote_retrieve_body(wp_safe_remote_get($attachment['url']));
}
if (empty($file_content)) {
return false;
}
$upload = wp_upload_bits($filename, null, $file_content);
$post = ['post_title' => $filename, 'guid' => $upload['url']];
$info = wp_check_filetype($upload['file']);
if ($info) {
$post['post_mime_type'] = $info['type'];
} else {
// For now just return the origin attachment
return $attachment;
//return new \WP_Error( 'attachment_processing_error', __( 'Invalid file type', 'elementor' ) );
}
$post_id = wp_insert_attachment($post, $upload['file']);
wp_update_attachment_metadata($post_id, wp_generate_attachment_metadata($post_id, $upload['file']));
update_post_meta($post_id, '_elementor_source_image_hash', $this->_get_hash_image($attachment['url']));
$new_attachment = ['id' => $post_id, 'url' => $upload['url']];
$this->_replace_image_ids[$attachment['id']] = $new_attachment;
return $new_attachment;
}
示例9: post_acme_article
function post_acme_article($post)
{
//if($post['post_title']) {
$post_data = array('post_title' => wp_strip_all_tags($post['post_title']), 'post_content' => $post['post_desc'], 'tax_input' => array('article_country_cat' => $post['post_country'], 'article_cat' => $post['post_category']), 'post_status' => $post['save'] != '' ? 'draft' : 'publish', 'post_type' => $post['custom_post_type'], 'post_author' => get_current_user_id());
$post_id = wp_insert_post($post_data);
// add_post_meta( $post_id, '_custom_image_link', $post['paste_featured_img'], true );
if (!empty($_FILES['post_featured_img']['name'])) {
$uploaddir = wp_upload_dir();
$file = $_FILES["post_featured_img"]["name"];
$uploadfile = $uploaddir['path'] . '/' . basename($file);
move_uploaded_file($file, $uploadfile);
$filename = basename($uploadfile);
$wp_filetype = wp_check_filetype(basename($filename), null);
$attachment = array('post_mime_type' => $wp_filetype['type'], 'post_title' => preg_replace('/\\.[^.]+$/', '', $filename), 'post_content' => '', 'post_status' => 'inherit');
foreach ($_FILES as $file => $value) {
require_once ABSPATH . "wp-admin" . '/includes/image.php';
require_once ABSPATH . "wp-admin" . '/includes/file.php';
require_once ABSPATH . "wp-admin" . '/includes/media.php';
$attach_id = media_handle_upload($file, $post_id);
set_post_thumbnail($post_id, $attach_id);
update_post_meta($post_id, '_thumbnail_id', $attach_id);
}
$image_url = wp_get_attachment_url(get_post_thumbnail_id($post_id));
} else {
$image_url = '';
}
$result = array('status' => 'success', 'post_id' => $post_id, 'image_url' => $image_url, 'featured_img' => $image_url, 'msg' => 'Post Save.');
// }
// } else {
// $result = array( 'status' => 'error', 'post_id' => $post_id, 'image_url' => $image_url, 'featured_img' => $image_url, 'msg' => 'Please fill-up the required fields.');
// }
return $result;
}
示例10: post_attachment
static function post_attachment($download, $product_id)
{
// $filename should be the path to a file in the upload directory.
// example $filename = 'woocommerce_uploads/2015/07/aka_Matrix42_Tool_CleanDatabase_7.2.1.20150625.aka.zip';
$download = json_decode($download);
$filename = $download->file;
$web_url = $download->web_url;
$wp_upload_dir = wp_upload_dir();
$file = $wp_upload_dir['path'] . '/' . $filename;
//Check the type of file. We'll use this as the 'post_mime_type'.
$filetype = wp_check_filetype(basename($file), null);
$attachment = array('guid' => $wp_upload_dir['url'] . '/' . basename($file), 'post_mime_type' => $filetype['type'], 'post_title' => preg_replace('/\\.[^.]+$/', '', basename($file)), 'post_content' => '', 'post_status' => 'inherit');
// Insert the attachment.
$attach_id = wp_insert_attachment($attachment, $file, $product_id);
// Make sure that this file is included, as wp_generate_attachment_metadata() depends on it.
require_once ABSPATH . 'wp-admin/includes/image.php';
// Generate the metadata for the attachment, and update the database record.
$attach_data = wp_generate_attachment_metadata($attach_id, $file);
wp_update_attachment_metadata($attach_id, $attach_data);
// Get all existing post downloads and
$files = array();
$wc_product = wc_get_product($product_id);
$untyped_array_of_downloads = $wc_product->get_files();
foreach ($untyped_array_of_downloads as $download_key => $download_value) {
$download_name = $download_value['name'];
$download_url = $download_value['file'];
$files[md5($download_url)] = array('name' => $download_name, 'file' => $download_url);
}
// Extend the existing post downloads by the new one
$files[md5($download_url)] = array('name' => $filename, 'file' => $web_url);
// Update post meta (_downloadable_files)
update_post_meta($product_id, '_downloadable_files', $files);
return 1;
}
示例11: validate_file_upload
/**
*
*/
public static function validate_file_upload($file_upload_param)
{
$count = 0;
$max_file_size = 200000000;
$validation_errors = array();
if (!empty($_FILES)) {
foreach ($_FILES[$file_upload_param]['name'] as $filename) {
if ($_FILES[$file_upload_param]['tmp_name'][$count] != '') {
// Setup the array of supported file types. In this case, it's just PDF.
$supported_types = array('image/jpeg', 'image/png', 'image/gif', 'application/pdf', 'application/vnd.ms-excel', 'application/msword', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', 'application/vnd.openxmlformats-officedocument.wordprocessingml.document');
// Get the file type of the upload
$arr_file_type = wp_check_filetype(basename($_FILES[$file_upload_param]['name'][$count]));
$uploaded_type = $arr_file_type['type'];
// Check if the type is supported. If not, throw an error.
if (!in_array($uploaded_type, $supported_types)) {
$supported = 'Supported files types are: Ms Word, Ms Excel, PDF';
$validation_errors['file_upload_error_msg'][$count] = 'The type of file you have uploaded is not supported. ' . $filename . '. ' . $supported;
}
// Check file size
if ($_FILES[$file_upload_param]['size'][$count] > $max_file_size) {
$validation_errors['file_upload_error_msg'][$count] = 'The file size is beyond the allowed maximum of 20MB';
}
}
$count++;
}
}
return $validation_errors;
}
示例12: insert_attachment
/**
* Helper function: insert an attachment to test properties of.
*
* @param int $parent_post_id
* @param str path to image to use
* @param array $post_fields Fields, in the format to be sent to `wp_insert_post()`
* @return int Post ID of inserted attachment
*/
private function insert_attachment($parent_post_id = 0, $image = null, $post_fields = array())
{
$filename = rand_str() . '.jpg';
$contents = rand_str();
if ($image) {
// @codingStandardsIgnoreStart
$filename = basename($image);
$contents = file_get_contents($image);
// @codingStandardsIgnoreEnd
}
$upload = wp_upload_bits($filename, null, $contents);
$this->assertTrue(empty($upload['error']));
$type = '';
if (!empty($upload['type'])) {
$type = $upload['type'];
} else {
$mime = wp_check_filetype($upload['file']);
if ($mime) {
$type = $mime['type'];
}
}
$attachment = wp_parse_args($post_fields, array('post_title' => basename($upload['file']), 'post_content' => 'Test Attachment', 'post_type' => 'attachment', 'post_parent' => $parent_post_id, 'post_mime_type' => $type, 'guid' => $upload['url']));
// Save the data
$id = wp_insert_attachment($attachment, $upload['file'], $parent_post_id);
wp_update_attachment_metadata($id, wp_generate_attachment_metadata($id, $upload['file']));
return $id;
}
示例13: _createObject
/**
* Generates a new attachemnt.
*
* @since 1.0.0
*
* @access protected
* @param array $args The array of arguments to use during a new attachment creation.
* @return int The newly created attachment's ID on success, otherwise 0.
*/
protected function _createObject($args = array())
{
if (empty($args['post_mime_type'])) {
if (!empty($args['file']) && is_readable($args['file'])) {
$this->_debug('Reading mime type of the file: ' . $args['file']);
$filetype = wp_check_filetype(basename($args['file']), null);
if (!empty($filetype['type'])) {
$args['post_mime_type'] = $filetype['type'];
$this->_debug('Mime type found: ' . $filetype['type']);
} else {
$this->_debug('Mime type not found');
}
}
}
$attachment_id = wp_insert_attachment($args);
if ($attachment_id) {
$this->_debug('Generated attachment ID: %d (file: %s)', $attachment_id, !empty($args['file']) ? $args['file'] : 'not-provided');
$this->_debug('Generating attachment metadata');
$metadata = wp_generate_attachment_metadata($attachment_id, $args['file']);
if (is_wp_error($metadata)) {
$this->_debug('Attachment metadata generation failed with error [%s] %s', $metadata->get_error_code(), $metadata->get_error_message());
} elseif (empty($metadata)) {
$this->_debug('Attachment metadata generation failed');
} else {
wp_update_attachment_metadata($attachment_id, $metadata);
}
} else {
$this->_debug('Attachment generation failed');
}
return $attachment_id;
}
示例14: get_template_variables
function get_template_variables($instance, $args)
{
static $player_id = 1;
$poster = '';
$video_host = $instance['host_type'];
if ($video_host == 'self') {
if (!empty($instance['video']['self_video'])) {
// Handle an attachment video
$src = wp_get_attachment_url($instance['video']['self_video']);
$vid_info = wp_get_attachment_metadata($instance['video']['self_video']);
$video_type = 'video/' . empty($vid_info['fileformat']) ? '' : $vid_info['fileformat'];
} else {
if (!empty($instance['video']['self_video_fallback'])) {
// Handle an external URL video
$src = $instance['video']['self_video_fallback'];
$vid_info = wp_check_filetype(basename($instance['video']['self_video_fallback']));
$video_type = $vid_info['type'];
}
}
$poster = !empty($instance['video']['self_poster']) ? wp_get_attachment_url($instance['video']['self_poster']) : '';
} else {
$video_host = $this->get_host_from_url($instance['video']['external_video']);
$video_type = 'video/' . $video_host;
$src = !empty($instance['video']['external_video']) ? $instance['video']['external_video'] : '';
}
$return = array('player_id' => 'sow-player-' . $player_id++, 'host_type' => $instance['host_type'], 'src' => $src, 'video_type' => $video_type, 'is_skinnable_video_host' => $this->is_skinnable_video_host($video_host), 'poster' => $poster, 'autoplay' => !empty($instance['playback']['autoplay']), 'skin_class' => 'default');
// Force oEmbed for this video
if ($instance['host_type'] == 'external' && $instance['playback']['oembed']) {
$return['is_skinnable_video_host'] = false;
}
return $return;
}
示例15: uploadImage
static function uploadImage($img_url)
{
include_once ABSPATH . 'wp-admin/includes/file.php';
//Contains download_url
//Download $img_url
$temporary_file = download_url($img_url);
if (is_wp_error($temporary_file)) {
throw new Exception('Error: ' . $temporary_file->get_error_message());
} else {
$upload_dir = wp_upload_dir();
$local_img_path = $upload_dir['path'] . DIRECTORY_SEPARATOR . basename($img_url);
//Local name
$local_img_url = $upload_dir['url'] . '/' . basename($img_url);
$moved = @rename($temporary_file, $local_img_path);
if ($moved) {
$wp_filetype = wp_check_filetype(basename($img_url), null);
//Get the filetype to set the mimetype
$attachment = array('post_mime_type' => $wp_filetype['type'], 'post_title' => preg_replace('/\\.[^.]+$/', '', basename($img_url)), 'post_content' => '', 'post_status' => 'inherit');
$attach_id = wp_insert_attachment($attachment, $local_img_path);
//Insert the image in the database
require_once ABSPATH . 'wp-admin/includes/image.php';
$attach_data = wp_generate_attachment_metadata($attach_id, $local_img_path);
wp_update_attachment_metadata($attach_id, $attach_data);
//Update generated metadata
return array('id' => $attach_id, 'url' => $local_img_url);
}
}
if (file_exists($temporary_file)) {
unlink($temporary_file);
}
return null;
}