本文整理汇总了PHP中media_handle_upload函数的典型用法代码示例。如果您正苦于以下问题:PHP media_handle_upload函数的具体用法?PHP media_handle_upload怎么用?PHP media_handle_upload使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了media_handle_upload函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: 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'));
}
示例2: process_async_upload
public function process_async_upload()
{
header('Content-Type: text/html; charset=' . get_option('blog_charset'));
if (!current_user_can('upload_files')) {
wp_die(__('You do not have permission to upload files.'));
}
check_admin_referer($this->nonce_name);
try {
$attachment_id = media_handle_upload('async-upload', 0, [], ['mimes' => ['mp3|m4a' => 'audio/mpeg'], 'action' => $this->action]);
$this->exception_if_error($attachment_id);
require_once WPPPT_PLUGIN_PATH . '/migrations/functions.php';
$post_id = \WPPPT\create_new_post(get_post($attachment_id));
$this->exception_if_error($post_id);
$podcast_id = intval($_REQUEST['podcast_id']);
if (!empty($podcast_id)) {
p2p_create_connection('podcast_clip_to_podcast', array('from' => $post_id, 'to' => $podcast_id));
}
echo apply_filters('wpppt_async_upload', $attachment_id);
} catch (\Exception $e) {
echo '<div class="error-div error">
<a class="dismiss" href="#" onclick="jQuery(this).parents(\'div.media-item\').slideUp(200, function(){jQuery(this).remove();});">' . __('Dismiss') . '</a>
<strong>' . sprintf(__('“%s” has failed to upload.'), esc_html($_FILES['async-upload']['name'])) . '</strong><br />' . esc_html($e->getMessage()) . '</div>';
exit;
}
}
示例3: wpp_upload_image
function wpp_upload_image()
{
if (count($_FILES) === 1) {
$filetype = $_FILES['image']['type'];
$allowed = '/image\\/(?:jpeg|png)/';
if (preg_match($allowed, $filetype)) {
if (!function_exists('wp_generate_attachment_metadata')) {
require_once ABSPATH . "wp-admin" . '/includes/image.php';
require_once ABSPATH . "wp-admin" . '/includes/file.php';
require_once ABSPATH . "wp-admin" . '/includes/media.php';
}
foreach ($_FILES as $file => $array) {
if ($_FILES[$file]['error'] !== UPLOAD_ERR_OK) {
return "upload error : " . $_FILES[$file]['error'];
}
$attach_id = media_handle_upload($file, 0);
}
//check for errors
if (is_wp_error($attach_id)) {
echo 'false';
} else {
echo $attach_id;
}
} else {
_e('Please only use jpeg or png images!', 'wpp');
}
} else {
_e('Please select only ONE image!', 'wpp');
}
wp_die();
//immediately end our ajax response
}
示例4: theme_options_validate_callback_function
/**
* Faz a validação dos dados
*
*/
function theme_options_validate_callback_function($input)
{
if (!empty($_FILES['logo']['name'])) {
$allowed_file_types = array('jpg' => 'image/jpg', 'jpeg' => 'image/jpeg', 'gif' => 'image/gif', 'png' => 'image/png');
if (!in_array($_FILES['logo']['type'], $allowed_file_types)) {
wp_die(__('Sorry, this file type is not permitted for security reasons.'));
}
/*
* O antigo wp_handle_upload( $_FILES[$field], array( 'test_form' => false ) ); foi substituído
*/
$upload = media_handle_upload('logo', 0);
if ($upload) {
$input['logo'] = $upload;
}
} else {
if (isset($_POST['restore-default-image']) && $_POST['restore-default-image'] == 1) {
unset($input['logo']);
} else {
$logo = get_theme_option('logo');
if (!empty($logo)) {
$input['logo'] = get_theme_option('logo');
}
}
}
return $input;
}
示例5: upload
/**
* Uploads file
*
* @param string Key to $_FILES
* @return int Attachment ID
*
* @abstract
*/
static function upload($file)
{
if (!isset($file)) {
return;
}
$file = media_handle_upload($file, 0, array(), array('test_form' => false, 'mimes' => array('csv' => 'text/csv')));
return $file;
}
示例6: handle_file
/**
* Upload file and return relevant attachment info
*
* @param string $value
* @param int $field_id
* @since 6.4
* @return array|int
*/
public function handle_file($value, $field_id)
{
$slug = get_post_meta($field_id, 'ccf_field_slug', true);
$file_id = media_handle_upload('ccf_field_' . $slug, 0);
if (is_wp_error($file_id)) {
return 0;
}
$url = wp_get_attachment_url($file_id);
return array('id' => $file_id, 'url' => $url, 'file_name' => basename($url));
}
示例7: wplf_ajax_submit_handler
function wplf_ajax_submit_handler()
{
$return = new stdClass();
$return->ok = 1;
// allow user to pre-process the post fields
do_action('wplf_pre_validate_submission');
// validate form fields
// @see: wplf-form-validation.php
$return = apply_filters('wplf_validate_submission', $return);
if ($return->ok) {
// form existence has already been validated via filters
$form = get_post(intval($_POST['_form_id']));
// the title is the value of whatever the first field was in the form
$title_format = get_post_meta($form->ID, '_wplf_title_format', true);
// substitute the %..% tags with field values
$post_title = $title_format;
preg_match_all('/%(.+?)%/', $post_title, $toks);
foreach ($toks[1] as $tok) {
$replace = '';
if (array_key_exists($tok, $_POST)) {
$replace = sanitize_text_field($_POST[$tok]);
}
$post_title = preg_replace('/%.+?%/', $replace, $post_title, 1);
}
// create submission post
$post_id = wp_insert_post(array('post_title' => $post_title, 'post_status' => 'publish', 'post_type' => 'wplf-submission'));
// add submission data as meta values
foreach ($_POST as $key => $value) {
if (!is_array($value)) {
add_post_meta($post_id, $key, esc_html($value), true);
} else {
add_post_meta($post_id, $key, esc_html(json_encode($value)), true);
}
}
// handle files
foreach ($_FILES as $key => $file) {
// Is this enough security wise?
// Currenly only supports 1 file per input
$attach_id = media_handle_upload($key, 0, array(), array("test_form" => false));
add_post_meta($post_id, $key, wp_get_attachment_url($attach_id));
add_post_meta($post_id, $key . "_attachment", $attach_id);
}
$return->submission_id = $post_id;
$return->submission_title = $post_title;
$return->form_id = $form->ID;
// return the success message for the form
$return->success = apply_filters('the_content', get_post_meta($form->ID, '_wplf_thank_you', true));
// allow user to attach custom actions after the submission has been received
// these could be confirmation emails, additional processing for the submission fields, e.g.
do_action('wplf_post_validate_submission', $return);
}
// respond with json
wp_send_json($return);
wp_die();
}
示例8: anno_popup_images_iframe_html
function anno_popup_images_iframe_html()
{
$errors = array();
if (isset($_POST['html-upload']) && !empty($_FILES)) {
check_admin_referer('media-form');
// Upload File button was clicked
$id = media_handle_upload('async-upload', $_REQUEST['post_id']);
unset($_FILES);
if (is_wp_error($id)) {
$errors['upload_error'] = $id;
$id = false;
}
}
global $tab;
$post_id = anno_get_post_id();
$attachments = get_posts(array('post_type' => 'attachment', 'posts_per_page' => -1, 'post_parent' => $post_id, 'post_mime_type' => 'image', 'order' => 'ASC'));
?>
<body id="anno-popup-images">
<div id="anno-popup-images-inside" class="anno-mce-popup">
<div class="anno-mce-popup-fields">
<?php
if (!empty($id)) {
if (is_wp_error($id)) {
echo '<div id="media-upload-error">' . esc_html($id->get_error_message()) . '</div>';
exit;
}
}
?>
<table class="anno-images">
<thead>
<tr>
<th scope="col" class="img-list-img"></th>
<th scope="col" class="img-list-title"></th>
<th scope="col" class="img-list-actions"></th>
</tr>
</thead>
<tbody id="media-items">
<?php
foreach ($attachments as $attachment_key => $attachment) {
anno_popup_images_row_display($attachment);
anno_popup_images_row_edit($attachment);
}
?>
</tbody>
</table>
<?php
anno_upload_form();
?>
</div>
</body>
<?php
}
示例9: mapasdevista_save_pins
/**
* Create or update pins.
*/
function mapasdevista_save_pins()
{
$error = array();
if (isset($_POST['submit_pin']) && $_POST['submit_pin'] === 'new') {
if (isset($_FILES['pin_file']) && $_FILES['pin_file']['size'] > 0) {
include ABSPATH . 'wp-admin/includes/file.php';
// para funcionar o
include ABSPATH . 'wp-admin/includes/image.php';
// media_handle_upload
include ABSPATH . 'wp-admin/includes/media.php';
//
$r = media_handle_upload('pin_file', NULL);
if (is_wp_error($r)) {
function mapasdevista_save_pin_error_notice()
{
echo '<div class="error"><p>' . __('Could not create directory.') . '</p></div>';
}
add_action('all_admin_notices', 'mapasdevista_save_pin_error_notice');
} else {
update_post_meta($r, '_pin_anchor', array('x' => 0, 'y' => 0));
wp_redirect(add_query_arg(array('action' => 'edit', 'pin' => $r)));
}
}
} elseif (isset($_POST['submit_pin']) && $_POST['submit_pin'] === 'edit') {
if (isset($_GET['pin']) && is_numeric($_GET['pin'])) {
$pin_id = intval(sprintf("%d", $_GET['pin']));
if (isset($_POST['pin_anchor']) && preg_match('/^([0-9]+),([0-9]+)$/', $_POST['pin_anchor'], $coords)) {
$anchor = array('x' => intval($coords[1]), 'y' => intval($coords[2]));
update_post_meta($pin_id, '_pin_anchor', $anchor);
}
if (isset($_POST['pin_clickable']) && $_POST['pin_clickable'] === 'no') {
update_post_meta($pin_id, '_pin_clickable', 'no');
} else {
delete_post_meta($pin_id, '_pin_clickable');
}
wp_redirect(add_query_arg(array('action' => 'edit', 'pin' => $pin_id)));
}
} else {
if (isset($_GET['action']) && $_GET['action'] === 'delete' && isset($_GET['pin'])) {
if (isset($_GET['pin']) && is_numeric($_GET['pin'])) {
$pin_id = intval(sprintf("%d", $_GET['pin']));
$pin = get_post($pin_id);
if ($pin) {
remove_pin($pin_id);
wp_redirect(admin_url('admin.php?page=mapasdevista_pins_page&msg=pin-deleted'));
die;
}
}
wp_redirect(admin_url('admin.php?page=mapasdevista_pins_page&msg=pin-does-not-exist'));
die;
}
}
}
示例10: set_feautured_image
public function set_feautured_image($data, $item_id)
{
if ($item_id) {
require_once ABSPATH . 'wp-admin/includes/image.php';
require_once ABSPATH . 'wp-admin/includes/file.php';
require_once ABSPATH . 'wp-admin/includes/media.php';
$this->attachment_id = media_handle_upload($data, $item_id);
set_post_thumbnail($item_id, $this->attachment_id);
} else {
return false;
}
}
示例11: handle_file
/**
* Upload file and return relevant attachment info
*
* @param string $value
* @param int $field_id
* @since 6.4
* @return array|int
*/
public function handle_file($value, $field_id)
{
require_once trailingslashit(ABSPATH) . 'wp-admin/includes/file.php';
require_once trailingslashit(ABSPATH) . 'wp-admin/includes/image.php';
require_once trailingslashit(ABSPATH) . 'wp-admin/includes/media.php';
$slug = get_post_meta($field_id, 'ccf_field_slug', true);
$file_id = media_handle_upload('ccf_field_' . $slug, 0);
if (is_wp_error($file_id)) {
return 0;
}
$url = wp_get_attachment_url($file_id);
return array('id' => $file_id, 'url' => $url, 'file_name' => basename($url));
}
示例12: upload
/**
* <input type="file" name="my_image_upload" accept="image/jpeg,image/png,image/gif"/>
* @return boolean/int
*/
public function upload()
{
require_once ABSPATH . 'wp-admin/includes/image.php';
require_once ABSPATH . 'wp-admin/includes/file.php';
require_once ABSPATH . 'wp-admin/includes/media.php';
// Позволим WordPress перехвалить загрузку.
// не забываем указать атрибут name поля input - 'my_image_upload'
$attachment_id = media_handle_upload('my_image_upload', 0);
if (is_wp_error($attachment_id)) {
return FALSE;
} else {
return $attachment_id;
}
}
示例13: uploadImage
public function uploadImage($uploadname, $post_id = 0, $meta_key = false)
{
if (isset($_FILES[$uploadname]) && !empty($_FILES[$uploadname])) {
require_once ABSPATH . 'wp-admin/includes/image.php';
require_once ABSPATH . 'wp-admin/includes/file.php';
require_once ABSPATH . 'wp-admin/includes/media.php';
$attachment_id = media_handle_upload($uploadname, $post_id);
if (is_wp_error($attachment_id)) {
do_action('Error_throw', array($uploadname => $attachment_id->get_error_message()));
} elseif ($post_id > 0) {
self::setImage($post_id, $attachment_id, $meta_key);
}
}
}
示例14: attach
public function attach()
{
$attachment_ids = array();
$original_files = $_FILES;
/*
* Get file upload global settings
*/
$multiple = get_option('rm_option_allow_multiple_file_uploads');
/*
* Handling multiple attachments
*/
if ($multiple == "yes") {
foreach ($_FILES as $f_name => $name) {
// var_dump($f_name);
$files = $_FILES[$f_name];
foreach ($files['name'] as $key => $value) {
if ($files['name'][$key]) {
$file = array('name' => $files['name'][$key], 'type' => $files['type'][$key], 'tmp_name' => $files['tmp_name'][$key], 'error' => $files['error'][$key], 'size' => $files['size'][$key]);
$_FILES = array($f_name => $file);
foreach ($_FILES as $file => $array) {
$attach_id = $this->media_handle_attachment($file, 0);
if (is_wp_error($attach_id)) {
break;
} else {
$attachment_ids[$f_name][] = $attach_id;
}
}
}
$_FILES = $original_files;
}
}
} else {
/*
* Handling single attachment
*/
foreach ($_FILES as $key => $file) {
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($key, 0);
if (is_wp_error($attach_id)) {
break;
} else {
$attachment_ids[$key] = $attach_id;
}
}
}
return $attachment_ids;
}
示例15: upload_file
public function upload_file()
{
require_once ABSPATH . "wp-admin" . '/includes/image.php';
require_once ABSPATH . "wp-admin" . '/includes/file.php';
require_once ABSPATH . "wp-admin" . '/includes/media.php';
if ($_FILES) {
foreach ($_FILES as $file => $array) {
if ($_FILES[$file]['error'] !== UPLOAD_ERR_OK) {
echo "upload error : " . $_FILES[$file]['error'];
die;
}
return media_handle_upload($file, 0);
}
}
}