/** * Upload * Ajax callback function * * @return string Error or (XML-)response */ static function handle_upload() { check_admin_referer('rwmb-upload-images_' . $_REQUEST['field_id']); $post_id = 0; if (is_numeric($_REQUEST['post_id'])) { $post_id = (int) $_REQUEST['post_id']; } // You can use WP's wp_handle_upload() function: $file = $_FILES['async-upload']; $file_attr = wp_handle_upload($file, array('test_form' => true, 'action' => 'plupload_image_upload')); $attachment = array('guid' => $file_attr['url'], 'post_mime_type' => $file_attr['type'], 'post_title' => preg_replace('/\\.[^.]+$/', '', basename($file['name'])), 'post_content' => '', 'post_status' => 'inherit'); // Adds file as attachment to WordPress $id = wp_insert_attachment($attachment, $file_attr['file'], $post_id); if (!is_wp_error($id)) { wp_update_attachment_metadata($id, wp_generate_attachment_metadata($id, $file_attr['file'])); // Save file ID in meta field if (isset($_REQUEST['field_id'])) { add_post_meta($post_id, $_REQUEST['field_id'], $id, false); } $response = new WP_Ajax_Response(); $response->add(array('what' => 'rwmb_image_response', 'data' => self::img_html($id))); $response->send(); } exit; }
/** * @param array $post * @param array $files */ public function bind(array $post, array $files = array()) { parent::bind($post, $files); // Remove the old image. if (isset($post['ab_remove_logo']) && file_exists(get_option('ab_settings_company_logo_path'))) { unlink(get_option('ab_settings_company_logo_path')); update_option('ab_settings_company_logo_path', ''); update_option('ab_settings_company_logo_url', ''); } // And add new. if (isset($files['ab_settings_company_logo']) && $files['ab_settings_company_logo']['tmp_name']) { if (in_array($files['ab_settings_company_logo']['type'], array("image/gif", "image/jpeg", "image/png"))) { $uploaded = wp_handle_upload($files['ab_settings_company_logo'], array('test_form' => false)); if ($uploaded) { $editor = wp_get_image_editor($uploaded['file']); $editor->resize(200, 200); $editor->save($uploaded['file']); $this->data['ab_settings_company_logo_path'] = $uploaded['file']; $this->data['ab_settings_company_logo_url'] = $uploaded['url']; // Remove old image. if (file_exists(get_option('ab_settings_company_logo_path'))) { unlink(get_option('ab_settings_company_logo_path')); } } } } }
$field_name = $option_group . '[' . $name . ']'; $value = $option_group_value[$name]; } else { $value = get_option($option_group . '_values'); $this->option_group = $option_group; $field_name = $option_group . '_values[' . $name . ']'; $value = $value[$name]; } $this->field_name = $field_name; $this->value = $value; $this->wp_name = $name; $field_name = $tk_form_instance_option_group . '_values[' . $name . ']'; $this->delete = $delete; $this->insert_attachement = $insert_attachement; parent::__construct($field_name, $args); } function validate_actions($input) { global $tk_form_instance_option_group; // If error occured if ($_FILES[$tk_form_instance_option_group . '_values']['error'][$this->wp_name] != 0) { $input[$this->wp_name] = $this->value; } else { // Storing new file $file['name'] = $_FILES[$tk_form_instance_option_group . '_values']['name'][$this->wp_name]; $file['type'] = $_FILES[$tk_form_instance_option_group . '_values']['type'][$this->wp_name]; $file['tmp_name'] = $_FILES[$tk_form_instance_option_group . '_values']['tmp_name'][$this->wp_name]; $file['error'] = $_FILES[$tk_form_instance_option_group . '_values']['error'][$this->wp_name]; $file['size'] = $_FILES[$tk_form_instance_option_group . '_values']['size'][$this->wp_name]; // Deleting old file
function process() { if (isset($_POST['upload'])) { check_admin_referer('thesis-favicon-upload', '_wpnonce-thesis-favicon-upload'); #wp $overrides = array('test_form' => false); $file = wp_handle_upload($_FILES['import'], $overrides); #wp if (isset($file['error'])) { wp_die($file['error'], __('Favicon Upload Error', 'thesis')); } #wp if ($file['type'] == 'image/x-icon' || $file['type'] == 'image/png') { $this->url = $file['url']; $this->save($file['file']); } else { $this->error = true; } } elseif ($_GET['remove']) { check_admin_referer('thesis-remove-favicon'); #wp unset($this->favicon); delete_option('thesis_favicon'); #wp $this->removed = true; } }
/** * @param array $post * @param array $files */ public function bind(array $post, array $files = array()) { parent::bind($post, $files); // remove the old image if (isset($post['ab_remove_logo']) && file_exists(get_option('ab_settings_company_logo_path'))) { unlink(get_option('ab_settings_company_logo_path')); update_option('ab_settings_company_logo_path', ''); update_option('ab_settings_company_logo_url', ''); } // and add new if (isset($files['ab_settings_company_logo']) && $files['ab_settings_company_logo']['tmp_name']) { if (in_array($files['ab_settings_company_logo']['type'], array("image/gif", "image/jpeg", "image/png"))) { $movefile = wp_handle_upload($files['ab_settings_company_logo'], array('test_form' => false)); if ($movefile) { $imageResize = new AB_ImageResize($movefile['file']); $imageResize->resizeImage(150, 150); $imageResize->saveImage($movefile['file']); $this->data['ab_settings_company_logo_path'] = $movefile['file']; $this->data['ab_settings_company_logo_url'] = $movefile['url']; // remove the old image if (file_exists(get_option('ab_settings_company_logo_path'))) { unlink(get_option('ab_settings_company_logo_path')); } } } } }
/** * Security checks for image upload form * @since 0.1 */ function wppb_image_upload_form_check() { // Check nonce - security protection to prevent creation and deletion of files by untrusted users if (!empty($_POST) and check_admin_referer('wppb_upload_image', 'image')) { // Upload file $data = $_FILES['upload_file']; if ('' != $data['name']) { $ext = substr(strrchr($data['name'], '.'), 1); // Grab extension $ext = strtolower($ext); // Convert extension to lower case // Spit an error out when not an image - would be better to send admin notice instead if ($ext != 'jpeg' and $ext != 'jpg' and $ext != 'gif' and $ext != 'png') { die('Only jpg, gif or png files are allowed to be uploaded!'); // Kill execution so they get to see the error } // Save file to disk add_filter('upload_dir', 'wppb_image_uploads_folder'); $overrides = array('test_form' => false); $file = wp_handle_upload($data, $overrides); remove_filter('upload_dir', 'wppb_image_uploads_folder'); } // Delete file if (isset($_POST['delete_file'])) { unlink(wppb_storage_folder('images') . '/' . $_POST['delete_file']); } } }
function nev_admin_upload_file() { if (!function_exists('wp_handle_upload')) { require_once ABSPATH . 'wp_admin/includes/file.php'; } if (empty($_FILES['file_path'])) { return; } $uploadedFile = $_FILES['file_path']; $uploaded_overrides = array('test_form' => false); $moveFile = wp_handle_upload($uploadedFile, $uploaded_overrides); if (!empty($moveFile['error'])) { echo $moveFile['error']; return; } if ($moveFile) { $wp_filetype = $moveFile['type']; $filename = $moveFile['file']; $wp_upload_dir = wp_upload_dir(); $attachment = array('guid' => $wp_upload_dir['url'] . '/' . basename($filename), 'post_mime_type' => $wp_filetype, 'post_title' => preg_replace('/\\.[^.]+$/', '', basename($filename)), 'post_content' => '', 'post_status' => 'inherit'); $attach_id = wp_insert_attachment($attachment, $filename); $file_path = get_attached_file($attach_id); update_option("nev-file_path", $file_path); echo "File uploaded to " . $file_path; } else { echo "Failed!"; } }
function epanel_ajax_callback() { global $wpdb; // this is how you get access to the database if($_POST['type']){ $save_type = $_POST['type']; }else $save_type = null; //Uploads if($save_type == 'upload'){ $clickedID = $_POST['data']; // Acts as the name $filename = $_FILES[$clickedID]; $filename['name'] = preg_replace('/[^a-zA-Z0-9._\-]/', '', $filename['name']); $override['test_form'] = false; $override['action'] = 'wp_handle_upload'; $uploaded_file = wp_handle_upload($filename,$override); $upload_tracking[] = $clickedID; epanel_update_option( $clickedID , $uploaded_file['url'] ); if(!empty($uploaded_file['error'])) {echo 'Upload Error: ' . $uploaded_file['error']; } else { echo $uploaded_file['url']; } // Is the Response } elseif($save_type == 'image_reset'){ $id = $_POST['data']; // Acts as the name epanel_update_option($id, null); } die(); }
/** * Upload an image via plupload. * * @return */ function charitable_plupload_image_upload() { $post_id = (int) filter_input(INPUT_POST, 'post_id', FILTER_SANITIZE_NUMBER_INT); $field_id = (string) filter_input(INPUT_POST, 'field_id'); check_ajax_referer('charitable-upload-images-' . $field_id); $file = $_FILES['async-upload']; $file_attr = wp_handle_upload($file, array('test_form' => false)); if (isset($file_attr['error'])) { wp_send_json_error($file_attr); } $attachment = array('guid' => $file_attr['url'], 'post_mime_type' => $file_attr['type'], 'post_title' => preg_replace('/\\.[^.]+$/', '', basename($file['name'])), 'post_content' => '', 'post_status' => 'inherit'); /** * Insert the file as an attachment. */ $attachment_id = wp_insert_attachment($attachment, $file_attr['file'], $post_id); if (is_wp_error($attachment_id)) { wp_send_json_error(); } wp_update_attachment_metadata($attachment_id, wp_generate_attachment_metadata($attachment_id, $file_attr['file'])); $size = (string) filter_input(INPUT_POST, 'size'); $max_uploads = (int) filter_input(INPUT_POST, 'max_uploads', FILTER_SANITIZE_NUMBER_INT); if (!$size) { $size = 'thumbnail'; } ob_start(); charitable_template('form-fields/picture-preview.php', array('image' => $attachment_id, 'field' => array('key' => $field_id, 'size' => $size, 'max_uploads' => $max_uploads))); wp_send_json_success(ob_get_clean()); }
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')); } }
/** * Process the upload. * * @since 1.0.0 */ public function handle_upload() { // Make sure all files are allowed if (!$this->check_file_type($_FILES['qqfile']['name'])) { return array('success' => false); } // if() // Get size and name if (!function_exists('wp_handle_upload')) { require_once ABSPATH . 'wp-admin/includes/file.php'; } $uploadedfile = $_FILES['qqfile']; $upload_overrides = array('test_form' => false); $movefile = wp_handle_upload($uploadedfile, $upload_overrides); if ($movefile) { $wp_upload_dir = wp_upload_dir(); $filename = str_replace($wp_upload_dir['url'] . '/', '', $movefile['url']); $attachment = $this->add_attachment($movefile['url'], $movefile['file']); $feat_image = wp_get_attachment_url($attachment); $img = vt_resize('', $feat_image, 200, 150, true); return array('success' => $movefile, 'attachmentId' => $attachment, 'attachment_url' => $img['url']); } else { return array('success' => false); } return array('success' => false); }
function atp_plupload_action() { // check ajax noonce $imgid = $_POST["imgid"]; check_ajax_referer($imgid . 'pluploadan'); $post_id = isset($_POST['post_id']) ? intval($_POST['post_id']) : 0; // handle file upload $filename = $_FILES[$imgid . 'async-upload']['name']; $status = wp_handle_upload($_FILES[$imgid . 'async-upload'], array('test_form' => true, 'action' => 'plupload_action')); if (!isset($status['file'])) { continue; } $file_name = $status['file']; $name_parts = pathinfo($file_name); $name = trim(substr($filename, 0, -(1 + strlen($name_parts['extension'])))); $attachment = array('post_mime_type' => $status['type'], 'guid' => $status['url'], 'post_parent' => $post_id, 'post_title' => $name, 'post_content' => ''); $id = wp_insert_attachment($attachment, $file_name, $post_id); if (!is_wp_error($id)) { wp_update_attachment_metadata($id, wp_generate_attachment_metadata($id, $file_name)); $new[] = $id; } // send the uploaded file url in response $upload_path = wp_get_attachment_url($id, true); if (preg_match_all('/[^\\?]+\\.(jpg|JPG|jpe|JPE|jpeg|JPEG|gif|GIF|png|PNG)/', $upload_path, $matches)) { $image_attributes = wp_get_attachment_image_src($id, 'thumbnail'); // returns an array $uplaod_url = $image_attributes[0]; } else { $uplaod_url = $upload_path; } $imagetest = array('url' => $status['url'], 'link' => get_edit_post_link($id), 'audioid' => $id, 'name' => $name, 'img' => $uplaod_url); echo json_encode($imagetest); //print_r ($imagetest); exit; }
function theme_add_admin() { global $themename, $shortname, $options; if ($_GET['page'] == basename(__FILE__)) { if ('save' == $_REQUEST['action']) { foreach ($options as $value) { if ($value['type'] == 'upload') { $file_uploaded = $_FILES[$value['id']]; if ($file_uploaded && $file_uploaded['error'] == 0) { $overrides = array('test_form' => false); $file = wp_handle_upload($file_uploaded, $overrides); update_option($value['id'], $file['url']); } } elseif (isset($_REQUEST[$value['id']])) { update_option($value['id'], $_REQUEST[$value['id']]); } } header("Location: themes.php?page=theme-options.php&saved=true"); die; } else { if ('reset' == $_REQUEST['action']) { foreach ($options as $value) { delete_option($value['id']); } header("Location: themes.php?page=theme-options.php&reset=true"); die; } } } add_theme_page($themename . " Options", "" . $themename . " Options", 'edit_themes', basename(__FILE__), 'theme_admin'); }
/** * Upload * Ajax callback function * * @return error or (XML-)response */ static function handle_upload() { header('Content-Type: text/html; charset=UTF-8'); if (!defined('DOING_AJAX')) { define('DOING_AJAX', true); } check_ajax_referer('plupload_image'); $post_id = 0; if (is_numeric($_REQUEST['post_id'])) { $post_id = (int) $_REQUEST['post_id']; } // you can use WP's wp_handle_upload() function: $file = $_FILES['async-upload']; $file_attr = wp_handle_upload($file, array('test_form' => true, 'action' => 'plupload_image_upload')); $attachment = array('post_mime_type' => $file_attr['type'], 'post_title' => preg_replace('/\\.[^.]+$/', '', basename($file['name'])), 'post_content' => '', 'post_status' => 'inherit'); // Adds file as attachment to WordPress $id = wp_insert_attachment($attachment, $file_attr['file'], $post_id); if (!is_wp_error($id)) { $response = new WP_Ajax_Response(); wp_update_attachment_metadata($id, wp_generate_attachment_metadata($id, $file_attr['file'])); if (isset($_REQUEST['field_id'])) { // Save file ID in meta field add_post_meta($post_id, $_REQUEST['field_id'], $id, false); } $response->add(array('what' => 'rwmb_image_response', 'data' => self::img_html($id))); $response->send(); } // faster than die(); exit; }
$skip_javascripts[] = ' $( "#skip_filepreview_' . $this->params['id'] . '" ).attr( "src", $( "#skip_filename_' . $this->params['id'] . ' a" ).attr( "href" ) ); '; $html_before = '<div class="skip_file ui-state-default ui-corner-all">'; $html_before .= '<div class="skip_filepreview">'; $html_before .= '<img id="skip_filepreview_' . $this->params['id'] . '" class="skip_filepreview_image" />'; if (isset($this->value['url'])) { $html_before .= '<div class="skip_filename" id="skip_filename_' . $this->params['id'] . '"><a href="' . $file_url . '" target="_blank">' . basename($file_path) . '</a></div>'; } $html_before .= '</div>'; $html_before .= '<div class="skip_fileuploader">'; $html_after = '</div></div>'; $this->before($html_before); $this->after($html_after); $this->add_param('class', 'skip_file_fileinput'); return parent::render(); } /** * Saving Editor field * @package Skip * @since 1.0 */ public function save() { if (array_key_exists('CONTENT_LENGTH', $_SERVER)) { if ($_SERVER['CONTENT_LENGTH'] > max_upload()) {
function cptImages_savefile($file, $name, $slug = false, $parent_post_id = 0, $content = '', $attachData = array()) { if (empty($file)) { return false; } if (!function_exists('wp_handle_upload')) { require_once ABSPATH . 'wp-admin/includes/file.php'; } $upload_overrides = array('test_form' => false); $movefile = wp_handle_upload($file, $upload_overrides); $name = 'wp-cpt-images image for ' . $name; if ($movefile && !isset($movefile['error'])) { $wp_filetype = $movefile['type']; $filename = $movefile['file']; $wp_upload_dir = wp_upload_dir(); $attachment = array('guid' => $wp_upload_dir['url'] . '/' . basename($filename), 'post_mime_type' => $wp_filetype, 'post_title' => $name, 'post_content' => $content, 'post_status' => 'inherit'); $attach_id = wp_insert_attachment($attachment, $filename, $parent_post_id); require_once ABSPATH . 'wp-admin/includes/image.php'; add_post_meta($attach_id, 'wp-cpt-image-attachment', $slug); foreach ($attachData as $key => $val) { add_post_meta($attach_id, $key, $val); } update_cpt_connection_link($slug, $attach_id); } return $attach_id; }
/** * Handle importer uploading and add attachment. * * @since 2.0.0 * * @return array Uploaded file's details on success, error message on failure */ function wp_import_handle_upload() { if ( !isset($_FILES['import']) ) { $file['error'] = __( 'File is empty. Please upload something more substantial. This error could also be caused by uploads being disabled in your php.ini or by post_max_size being defined as smaller than upload_max_filesize in php.ini.' ); return $file; } $overrides = array( 'test_form' => false, 'test_type' => false ); $_FILES['import']['name'] .= '.txt'; $file = wp_handle_upload( $_FILES['import'], $overrides ); if ( isset( $file['error'] ) ) return $file; $url = $file['url']; $type = $file['type']; $file = $file['file']; $filename = basename( $file ); // Construct the object array $object = array( 'post_title' => $filename, 'post_content' => $url, 'post_mime_type' => $type, 'guid' => $url, 'context' => 'import', 'post_status' => 'private' ); // Save the data $id = wp_insert_attachment( $object, $file ); // schedule a cleanup for one day from now in case of failed import or missing wp_import_cleanup() call wp_schedule_single_event( time() + DAY_IN_SECONDS, 'importer_scheduled_cleanup', array( $id ) ); return array( 'file' => $file, 'id' => $id ); }
/** * insert_panel_upload function. * * @access public * @return void */ public function insert_panel_upload() { check_ajax_referer('file-upload'); $status = wp_handle_upload($_FILES['async-upload'], array('test_form' => false)); echo $status['url']; die; }
/** * Set image * * @param string $keyImg * Key from the image * @param file $imgFile * The image * @throws Exception * @return void|string */ protected function setImage($keyImg, $imgFile) { // If it's false or null we have to remove it from the server if (!$imgFile || is_null($imgFile)) { return $this->removeImage($keyImg); } if (strpos($imgFile['name'], '.php') !== false) { throw new Exception('For security reasons, the extension ".php" cannot be in your file name.'); } $avatar = wp_handle_upload($_FILES[$keyImg], array('mimes' => array('jpg|jpeg|jpe' => 'image/jpeg', 'gif' => 'image/gif', 'png' => 'image/png'), 'test_form' => false, 'unique_filename_callback' => function ($dir, $name, $ext) use($keyImg) { $name = $base_name = sanitize_file_name($this->user_login . '_' . $keyImg); $number = 1; while (file_exists($dir . "/{$name}{$ext}")) { $name = $base_name . '_' . $number; $number++; } return $name . $ext; })); // Remove the last image $this->removeImage($keyImg); $metaValue = array(); $url_or_media_id = $avatar['url']; // Set the new image if (is_int($url_or_media_id)) { $metaValue['media_id'] = $url_or_media_id; $url_or_media_id = wp_get_attachment_url($url_or_media_id); } $metaValue['full'] = $url_or_media_id; return update_user_meta($this->ID, $keyImg, $metaValue); }
function validate_setting($plugin_options) { $keys = array_keys($_FILES); $i = 0; foreach ( $_FILES as $image ) { // if a files was upload if ($image['size']) { // if it is an image if ( preg_match('/(jpg|jpeg|png|gif)$/', $image['type']) ) { $override = array('test_form' => false); // save the file, and store an array, containing its location in $file $file = wp_handle_upload( $image, $override ); $plugin_options[$keys[$i]] = $file['url']; } else { // Not an image. $options = get_option('plugin_options'); $plugin_options[$keys[$i]] = $options[$logo]; // Die and let the user know that they made a mistake. wp_die('No image was uploaded.'); } } else { // else, the user didn't upload a file, retain the image that's already on file. $options = get_option('plugin_options'); $plugin_options[$keys[$i]] = $options[$keys[$i]]; } $i++; } return $plugin_options; }
function file_upload_example_options_page() { if (empty($_FILES)) { ?> <div> <h2>Upload a file here</h2> <form action="" method="post" enctype="multipart/form-data"> <?php wp_nonce_field('csv-import'); ?> <label for="file">Filename:</label> <input type="file" name="file" id="file"><br> <input type="submit" name="save" value="save"> </form> </div> <?php } else { if (!function_exists('wp_handle_upload')) { require_once ABSPATH . 'wp-admin/includes/file.php'; } $uploadedfile = $_FILES['file']; $upload_overrides = array('test_form' => false); $movefile = wp_handle_upload($uploadedfile, $upload_overrides); if ($movefile) { echo "File is valid, and was successfully uploaded.\n"; var_dump($movefile); // here you can do some stuff with this } else { echo "Possible file upload attack!\n"; } } }
function nebula_upload_to_media_library($filepath) { if (!file_exists($filepath) || strlen(trim($filepath)) == 0) { return; } $result = wp_handle_upload($filepath); }
public function check() { $v = ""; require_once ABSPATH . 'wp-admin/includes/file.php'; $msg = $_FILES[$this->name]["name"]; /* 予めいれておく。。 */ $upload_overrides = array('test_form' => false); $movefile = wp_handle_upload($_FILES[$this->name], $upload_overrides); if ($movefile) { if (!isset($movefile['error']) && isset($movefile['file']) && isset($movefile['url'])) { $_REQUEST[$this->name . '-file'] = $movefile['file']; $_REQUEST[$this->name . '-url'] = $movefile['url']; } } else { $tmpname = $this->name . '-tmp'; $upload_filename = $this->get($tmpname); if (isset($upload_filename) && $upload_filename != '') { $msg = $upload_filename; } else { // 添付ファイル指定がなかった場合 if (isset($sheet['check-must']) && $sheet['check-must'] != '') { // $msg = sprintf($this->errorfmt, $sheet['check-must']); // $this->errflag++; // 何かエラーを返すべきか。。 } } } return $v; }
public static function init() { if (!is_admin()) { return; } if (!isset($_FILES['podlove_import_tracking'])) { return; } set_time_limit(10 * MINUTE_IN_SECONDS); // allow xml+gz uploads add_filter('upload_mimes', function ($mimes) { return array_merge($mimes, array('xml' => 'application/xml', 'gz|gzip' => 'application/x-gzip')); }); require_once ABSPATH . '/wp-admin/includes/file.php'; $file = wp_handle_upload($_FILES['podlove_import_tracking'], array('test_form' => false)); if ($file && (!isset($file['error']) || !$file['error'])) { update_option('podlove_import_tracking_file', $file['file']); if (!($file = get_option('podlove_import_tracking_file'))) { return; } $importer = new \Podlove\Modules\ImportExport\Import\TrackingImporter($file); $importer->import(); } else { echo '<div class="error"><p>' . $file['error'] . '</p></div>'; } }
function validate_setting($wpspandntset) { $keys = array_keys($_FILES); $i = 0; foreach ($_FILES as $image) { // if a files was upload if ($image['size']) { // if it is an image if (preg_match('/(jpg|jpeg|png|gif)$/', $image['type'])) { $override = array('test_form' => false); // save the file, and store an array, containing its location in $file $file = wp_handle_upload($image, $override); $wpspandntset[$keys[$i]] = $file['url']; } else { // Not an image. $options = get_option('wpspandntset'); $wpspandntset[$keys[$i]] = $options[$logo]; wp_die('No image was uploaded.'); } } else { $options = get_option('wpspandntset'); $wpspandntset[$keys[$i]] = $options[$keys[$i]]; } $i++; } return $wpspandntset; }
function media_handle_upload($file_id, $post_id, $post_data = array()) { $overrides = array('test_form' => false); $file = wp_handle_upload($_FILES[$file_id], $overrides); if (isset($file['error'])) { return new wp_error('upload_error', $file['error']); } $url = $file['url']; $type = $file['type']; $file = $file['file']; $title = preg_replace('/\\.[^.]+$/', '', basename($file)); $content = ''; // use image exif/iptc data for title and caption defaults if possible if ($image_meta = @wp_read_image_metadata($file)) { if (trim($image_meta['title'])) { $title = $image_meta['title']; } if (trim($image_meta['caption'])) { $content = $image_meta['caption']; } } // Construct the attachment array $attachment = array_merge(array('post_mime_type' => $type, 'guid' => $url, 'post_parent' => $post_id, 'post_title' => $title, 'post_content' => $content), $post_data); // Save the data $id = wp_insert_attachment($attachment, $file, $post_parent); if (!is_wp_error($id)) { wp_update_attachment_metadata($id, wp_generate_attachment_metadata($id, $file)); } return $id; }
function my_save_extra_profile_fields($user_id) { if (!current_user_can('edit_user', $user_id)) { return false; } $upload = $_FILES['profile_image']; $uploads = wp_upload_dir(); if ($upload['tmp_name'] && file_is_displayable_image($upload['tmp_name'])) { // handle the uploaded file $overrides = array('test_form' => false); $file = wp_handle_upload($upload, $overrides); $file["file"] = $uploads["subdir"] . "/" . basename($file["url"]); if ($file) { //remove previous uploaded file $author_profile_image = $this->get_author_profile_image($user_id); @unlink($author_profile_image["file"]); update_user_meta($user_id, 'profile_image', $file); } } if (isset($_POST['remove_image'])) { $author_profile_image = $this->get_author_profile_image($user_id); @unlink($author_profile_image["file"]); update_user_meta($user_id, 'profile_image', false); } }
/** * Upload * Ajax callback function * * @return string Error or (XML-)response */ static function handle_upload() { global $wpdb; $post_id = isset($_REQUEST['post_id']) ? intval($_REQUEST['post_id']) : 0; $field_id = isset($_REQUEST['field_id']) ? $_REQUEST['field_id'] : ''; check_ajax_referer("rwmb-upload-images_{$field_id}"); // You can use WP's wp_handle_upload() function: $file = $_FILES['async-upload']; $file_attr = wp_handle_upload($file, array('test_form' => false)); //Get next menu_order $meta = get_post_meta($post_id, $field_id, false); if (empty($meta)) { $next = 0; } else { $meta = implode(',', (array) $meta); $max = $wpdb->get_var("\n\t\t\t\t\tSELECT MAX(menu_order) FROM {$wpdb->posts}\n\t\t\t\t\tWHERE post_type = 'attachment'\n\t\t\t\t\tAND ID in ({$meta})\n\t\t\t\t"); $next = is_numeric($max) ? (int) $max + 1 : 0; } $attachment = array('guid' => $file_attr['url'], 'post_mime_type' => $file_attr['type'], 'post_title' => preg_replace('/\\.[^.]+$/', '', basename($file['name'])), 'post_content' => '', 'post_status' => 'inherit', 'menu_order' => $next); // Adds file as attachment to WordPress $id = wp_insert_attachment($attachment, $file_attr['file'], $post_id); if (!is_wp_error($id)) { wp_update_attachment_metadata($id, wp_generate_attachment_metadata($id, $file_attr['file'])); // Save file ID in meta field add_post_meta($post_id, $field_id, $id, false); wp_send_json_success(self::img_html($id)); } exit; }
function sf_handle_img_upload($new_value, $old_value, $file_index, $delete_field) { if (isset($_POST[$delete_field]) && $_POST[$delete_field] == 'true') { return ''; } if (empty($_FILES) || !isset($_FILES[$file_index]) || 0 == $_FILES[$file_index]['size']) { return $old_value; } $overrides = array('test_form' => false); $file = wp_handle_upload($_FILES[$file_index], $overrides); if (isset($file['error'])) { wp_die($file['error']); } $url = $file['url']; $type = $file['type']; $file = $file['file']; $filename = basename($file); // Construct The Object Array $object = array('post_title' => $filename, 'post_content' => $url, 'post_mime_type' => $type, 'guid' => $url); // Save The Data $id = wp_insert_attachment($object, $file); // Add The Meta wp_update_attachment_metadata($id, wp_generate_attachment_metadata($id, $file)); do_action('wp_create_file_in_uploads', $file, $id); // For replication return esc_url($url); }
/** * @return void; */ public function admin_upload() { $mediaFile = array('error' => __('No files to upload', 'gummfw')); if ($this->data && $_FILES && $this->validates()) { $override = array('test_form' => false, 'action' => 'wp_handle_upload'); if ($mediaFile = wp_handle_upload($_FILES['Filedata'], $override)) { list($mediaFile['width'], $mediaFile['height']) = getimagesize($mediaFile['file']); } } $render = true; if (isset($this->data['_render'])) { if ($this->data['_render'] == 0) { $render = false; } elseif (strpos($this->data['_render'], 'x') !== false) { $render = $this->data['_render']; } } if ($render) { $this->set(compact('mediaFile', 'optionId', 'render')); $this->render(); } if ($this->RequestHandler->isAjax()) { if (!$render) { echo json_encode($mediaFile); } die; } }