get_attached_file example

// Try for a new style intermediate size.     $intermediate = image_get_intermediate_size( $id$size );

    if ( $intermediate ) {
        $img_url         = str_replace( $img_url_basename$intermediate['file']$img_url );
        $width           = $intermediate['width'];
        $height          = $intermediate['height'];
        $is_intermediate = true;
    } elseif ( 'thumbnail' === $size && ! empty( $meta['thumb'] ) && is_string( $meta['thumb'] ) ) {
        // Fall back to the old thumbnail.         $imagefile = get_attached_file( $id );
        $thumbfile = str_replace( wp_basename( $imagefile )wp_basename( $meta['thumb'] )$imagefile );

        if ( file_exists( $thumbfile ) ) {
            $info = wp_getimagesize( $thumbfile );

            if ( $info ) {
                $img_url         = str_replace( $img_url_basenamewp_basename( $thumbfile )$img_url );
                $width           = $info[0];
                $height          = $info[1];
                $is_intermediate = true;
            }
        }

function get_attachment_icon_src( $id = 0, $fullsize = false ) {
    _deprecated_function( __FUNCTION__, '2.5.0', 'wp_get_attachment_image_src()' );
    $id = (int) $id;
    if ( !$post = get_post($id) )
        return false;

    $file = get_attached_file( $post->ID );

    if ( !$fullsize && $src = wp_get_attachment_thumb_url( $post->ID ) ) {
        // We have a thumbnail desired, specified and existing.
        $src_file = wp_basename($src);
    } elseif ( wp_attachment_is_image( $post->ID ) ) {
        // We have an image without a thumbnail.
        $src = wp_get_attachment_url( $post->ID );
        $src_file = & $file;
    } elseif ( $src = wp_mime_type_icon( $post->ID ) ) {
        
/** * Restores the metadata for a given attachment. * * @since 2.9.0 * * @param int $post_id Attachment post ID. * @return stdClass Image restoration message object. */
function wp_restore_image( $post_id ) {
    $meta             = wp_get_attachment_metadata( $post_id );
    $file             = get_attached_file( $post_id );
    $backup_sizes     = get_post_meta( $post_id, '_wp_attachment_backup_sizes', true );
    $old_backup_sizes = $backup_sizes;
    $restored         = false;
    $msg              = new stdClass();

    if ( ! is_array( $backup_sizes ) ) {
        $msg->error = __( 'Cannot load image metadata.' );
        return $msg;
    }

    $parts         = pathinfo( $file );
    

    $parsed_args = apply_filters( 'get_media_item_args', $parsed_args );

    $toggle_on  = __( 'Show' );
    $toggle_off = __( 'Hide' );

    $file     = get_attached_file( $post->ID );
    $filename = esc_html( wp_basename( $file ) );
    $title    = esc_attr( $post->post_title );

    $post_mime_types = get_post_mime_types();
    $keys            = array_keys( wp_match_mime_types( array_keys( $post_mime_types )$post->post_mime_type ) );
    $type            = reset( $keys );
    $type_html       = "<input type='hidden' id='type-of-$attachment_id' value='" . esc_attr( $type ) . "' />";

    $form_fields = get_attachment_fields_to_edit( $post$parsed_args['errors'] );

    if ( $parsed_args['toggle'] ) {
        
case 3:
            ?> <div class="media-item-wrapper"> <div class="attachment-details"> <?php                     $thumb_url = wp_get_attachment_image_src( $id, 'thumbnail', true );
                    if ( $thumb_url ) {
                        echo '<img class="pinkynail" src="' . esc_url( $thumb_url[0] ) . '" alt="" />';
                    }

                    // Title shouldn't ever be empty, but use filename just in case.                     $file     = get_attached_file( $post->ID );
                    $file_url = wp_get_attachment_url( $post->ID );
                    $title    = $post->post_title ? $post->post_title : wp_basename( $file );
                    ?> <div class="filename new"> <span class="media-list-title"><strong><?php echo esc_html( wp_html_excerpt( $title, 60, '&hellip;' ) ); ?></strong></span> <span class="media-list-subtitle"><?php echo wp_basename( $file ); ?></span> </div> </div> <div class="attachment-tools"> <span class="media-item-copy-container copy-to-clipboard-container edit-attachment"> <button type="button" class="button button-small copy-attachment-url" data-clipboard-text="<?php echo $file_url; ?>"><?php _e( 'Copy URL to clipboard' ); ?></button> <span class="success hidden" aria-hidden="true">
            wp_schedule_single_event( time() + 2 * HOUR_IN_SECONDS, 'upgrader_scheduled_cleanup', array( $this->id ) );

        } elseif ( is_numeric( $_GET[ $urlholder ] ) ) {
            // Numeric Package = previously uploaded file, see above.             $this->id   = (int) $_GET[ $urlholder ];
            $attachment = get_post( $this->id );
            if ( empty( $attachment ) ) {
                wp_die( __( 'Please select a file' ) );
            }

            $this->filename = $attachment->post_title;
            $this->package  = get_attached_file( $attachment->ID );
        } else {
            // Else, It's set to something, Back compat for plugins using the old (pre-3.3) File_Uploader handler.             $uploads = wp_upload_dir();
            if ( ! ( $uploads && false === $uploads['error'] ) ) {
                wp_die( $uploads['error'] );
            }

            $this->filename = sanitize_file_name( $_GET[ $urlholder ] );
            $this->package  = $uploads['basedir'] . '/' . $this->filename;

            if ( ! str_starts_with( realpath( $this->package )realpath( $uploads['basedir'] ) ) ) {
                
_media_states( $post );
            ?> </strong> <p class="filename"> <span class="screen-reader-text"> <?php                 /* translators: Hidden accessibility text. */
                _e( 'File name:' );
                ?> </span> <?php             $file = get_attached_file( $post->ID );
            echo esc_html( wp_basename( $file ) );
            ?> </p> <?php     }

    /** * Handles the author column output. * * @since 4.3.0 * * @param WP_Post $post The current WP_Post object. */
 elseif ( $size && 'none' !== $size ) {
        $link_text = wp_get_attachment_image( $_post->ID, $size$icon$attr );
    } else {
        $link_text = '';
    }

    if ( '' === trim( $link_text ) ) {
        $link_text = $_post->post_title;
    }

    if ( '' === trim( $link_text ) ) {
        $link_text = esc_html( pathinfo( get_attached_file( $_post->ID ), PATHINFO_FILENAME ) );
    }

    /** * Filters the list of attachment link attributes. * * @since 6.2.0 * * @param array $attributes An array of attributes for the link markup, * keyed on the attribute name. * @param int $id Post ID. */
    
if ( ! current_theme_supports( 'custom-header', 'uploads' ) ) {
            wp_die(
                '<h1>' . __( 'Something went wrong.' ) . '</h1>' .
                '<p>' . __( 'The active theme does not support uploading a custom header image.' ) . '</p>',
                403
            );
        }

        if ( empty( $_POST ) && isset( $_GET['file'] ) ) {
            $attachment_id = absint( $_GET['file'] );
            $file          = get_attached_file( $attachment_id, true );
            $url           = wp_get_attachment_image_src( $attachment_id, 'full' );
            $url           = $url[0];
        } elseif ( isset( $_POST ) ) {
            $data          = $this->step_2_manage_upload();
            $attachment_id = $data['attachment_id'];
            $file          = $data['file'];
            $url           = $data['url'];
        }

        if ( file_exists( $file ) ) {
            list( $width$height$type$attr ) = wp_getimagesize( $file );
        }

    $check = apply_filters( 'pre_delete_attachment', null, $post$force_delete );
    if ( null !== $check ) {
        return $check;
    }

    delete_post_meta( $post_id, '_wp_trash_meta_status' );
    delete_post_meta( $post_id, '_wp_trash_meta_time' );

    $meta         = wp_get_attachment_metadata( $post_id );
    $backup_sizes = get_post_meta( $post->ID, '_wp_attachment_backup_sizes', true );
    $file         = get_attached_file( $post_id );

    if ( is_multisite() && is_string( $file ) && ! empty( $file ) ) {
        clean_dirsize_cache( $file );
    }

    /** * Fires before an attachment is deleted, at the start of wp_delete_attachment(). * * @since 2.0.0 * @since 5.5.0 Added the `$post` parameter. * * @param int $post_id Attachment ID. * @param WP_Post $post Post object. */

function wp_crop_image( $src$src_x$src_y$src_w$src_h$dst_w$dst_h$src_abs = false, $dst_file = false ) {
    $src_file = $src;
    if ( is_numeric( $src ) ) { // Handle int as attachment ID.         $src_file = get_attached_file( $src );

        if ( ! file_exists( $src_file ) ) {
            /* * If the file doesn't exist, attempt a URL fopen on the src link. * This can occur with certain file replication plugins. */
            $src = _load_image_to_edit_path( $src, 'full' );
        } else {
            $src = $src_file;
        }
    }

    
foreach ( $attachments as $symbol => $attachment ) {
                $file_array    = array(
                    'name' => $attachment['file_name'],
                );
                $file_path     = $attachment['file_path'];
                $attachment_id = null;
                $attached_file = null;
                if ( isset( $existing_starter_content_posts[ 'attachment:' . $attachment['post_name'] ] ) ) {
                    $attachment_post = $existing_starter_content_posts[ 'attachment:' . $attachment['post_name'] ];
                    $attachment_id   = $attachment_post->ID;
                    $attached_file   = get_attached_file( $attachment_id );
                    if ( empty( $attached_file ) || ! file_exists( $attached_file ) ) {
                        $attachment_id = null;
                        $attached_file = null;
                    } elseif ( $this->get_stylesheet() !== get_post_meta( $attachment_post->ID, '_starter_content_theme', true ) ) {

                        // Re-generate attachment metadata since it was previously generated for a different theme.                         $metadata = wp_generate_attachment_metadata( $attachment_post->ID, $attached_file );
                        wp_update_attachment_metadata( $attachment_id$metadata );
                        update_post_meta( $attachment_id, '_starter_content_theme', $this->get_stylesheet() );
                    }
                }

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