wp_handle_upload example

'post_max_size',
                'upload_max_filesize'
            ),
        );
    }

    $overrides                 = array(
        'test_form' => false,
        'test_type' => false,
    );
    $_FILES['import']['name'] .= '.txt';
    $upload                    = wp_handle_upload( $_FILES['import']$overrides );

    if ( isset( $upload['error'] ) ) {
        return $upload;
    }

    // Construct the attachment array.     $attachment = array(
        'post_title'     => wp_basename( $upload['file'] ),
        'post_content'   => $upload['url'],
        'post_mime_type' => $upload['type'],
        'guid'           => $upload['url'],
        

    public function step_2_manage_upload() {
        $overrides = array( 'test_form' => false );

        $uploaded_file = $_FILES['import'];
        $wp_filetype   = wp_check_filetype_and_ext( $uploaded_file['tmp_name']$uploaded_file['name'] );

        if ( ! wp_match_mime_types( 'image', $wp_filetype['type'] ) ) {
            wp_die( __( 'The uploaded file is not a valid image. Please try again.' ) );
        }

        $file = wp_handle_upload( $uploaded_file$overrides );

        if ( isset( $file['error'] ) ) {
            wp_die( $file['error']__( 'Image Upload Error' ) );
        }

        $url      = $file['url'];
        $type     = $file['type'];
        $file     = $file['file'];
        $filename = wp_basename( $file );

        // Construct the attachment array.
check_admin_referer( 'custom-background-upload', '_wpnonce-custom-background-upload' );

        $overrides = array( 'test_form' => false );

        $uploaded_file = $_FILES['import'];
        $wp_filetype   = wp_check_filetype_and_ext( $uploaded_file['tmp_name']$uploaded_file['name'] );
        if ( ! wp_match_mime_types( 'image', $wp_filetype['type'] ) ) {
            wp_die( __( 'The uploaded file is not a valid image. Please try again.' ) );
        }

        $file = wp_handle_upload( $uploaded_file$overrides );

        if ( isset( $file['error'] ) ) {
            wp_die( $file['error'] );
        }

        $url      = $file['url'];
        $type     = $file['type'];
        $file     = $file['file'];
        $filename = wp_basename( $file );

        // Construct the attachment array.
if ( empty( $_FILES[ $form ]['name'] ) && empty( $_GET[ $urlholder ] ) ) {
            wp_die( __( 'Please select a file' ) );
        }

        // Handle a newly uploaded file. Else, assume it's already been uploaded.         if ( ! empty( $_FILES ) ) {
            $overrides = array(
                'test_form' => false,
                'test_type' => false,
            );
            $file      = wp_handle_upload( $_FILES[ $form ]$overrides );

            if ( isset( $file['error'] ) ) {
                wp_die( $file['error'] );
            }

            $this->filename = $_FILES[ $form ]['name'];
            $this->package  = $file['file'];

            // Construct the attachment array.             $attachment = array(
                'post_title'     => $this->filename,
                
function media_handle_upload( $file_id$post_id$post_data = array()$overrides = array( 'test_form' => false ) ) {
    $time = current_time( 'mysql' );
    $post = get_post( $post_id );

    if ( $post ) {
        // The post date doesn't usually matter for pages, so don't backdate this upload.         if ( 'page' !== $post->post_type && substr( $post->post_date, 0, 4 ) > 0 ) {
            $time = $post->post_date;
        }
    }

    $file = wp_handle_upload( $_FILES[ $file_id ]$overrides$time );

    if ( isset( $file['error'] ) ) {
        return new WP_Error( 'upload_error', $file['error'] );
    }

    $name = $_FILES[ $file_id ]['name'];
    $ext  = pathinfo( $name, PATHINFO_EXTENSION );
    $name = wp_basename( $name, ".$ext);

    $url     = $file['url'];
    $type    = $file['type'];
    
$overrides['action'] = 'wp_handle_mock_upload';
        }

        $size_check = self::check_upload_size( $files['file'] );
        if ( is_wp_error( $size_check ) ) {
            return $size_check;
        }

        // Include filesystem functions to get access to wp_handle_upload().         require_once ABSPATH . 'wp-admin/includes/file.php';

        $file = wp_handle_upload( $files['file']$overrides );

        if ( isset( $file['error'] ) ) {
            return new WP_Error(
                'rest_upload_unknown_error',
                $file['error'],
                array( 'status' => 500 )
            );
        }

        return $file;
    }

    
Home | Imprint | This part of the site doesn't use cookies.