0

I have page and post in which user upload files as attachment and display link for donwload this files. How I can display after the size link type of uploaded file and the size?

After answer in this therad I have the code

$attachments = get_posts( array(
                            'post_type' => 'attachment',
                            'posts_per_page' => -1,
                            'post_parent' => $post->ID,
                            'exclude'     => get_post_thumbnail_id()
                        ) );

                if ( $attachments ) {
                    foreach ( $attachments as $attachment ) {
                        // Assuming $attachment_id contains the ID of the attachment
                        $attachment_metadata = wp_get_attachment_metadata($attachment->ID);

                        if ($attachment_metadata) {
                            // Get file size in bytes
                            $file_size = filesize(get_attached_file($attachment->ID));

                            // Convert file size to human-readable format
                            $file_size_human = size_format($file_size);

                            // Get file type
                            $file_type = $attachment_metadata['mime_type'];
                            //$file_type = get_post_mime_type($attachment->ID);
                            $file_url = wp_get_attachment_url( $attachment->ID);
                            $filetype = wp_check_filetype( $file_url );

                            // Display file size and type
                            //echo '<p>File Size: ' . $file_size_human . '</p>';
                            //echo '<p>File Type: ' . $filetype['ext'] . '</p>';
                            $link = wp_get_attachment_link($attachment->ID);
                            echo '<li>' . $link . '( ' . $file_size_human . ', ' . $filetype['ext'] .')</li>';
                        }

code works for me, but another problem is that attachment is display on function the_content(). How is the way to modify the_content to use only my own function for display links to attachment?

1
  • a note that this info is stored in the DB, not read from the file, so if you adjust the file manually via FTP it won't change when displayed on the frontend. I also don't see any code in your question for what you've done so far to display the attachments
    – Tom J Nowell
    Commented Mar 20 at 11:15

1 Answer 1

1

you can use within your page or post template and also replace $attachment_id with the ID of the attachment you want to display information for.

// Assuming $attachment_id contains the ID of the attachment
$attachment_metadata = wp_get_attachment_metadata($attachment_id);

if ($attachment_metadata) {
    // Get file size in bytes
    $file_size = filesize(get_attached_file($attachment_id));
    
    // Convert file size to human-readable format
    $file_size_human = size_format($file_size);
    
    // Get file type
    $file_type = $attachment_metadata['mime_type'];
    
    // Display file size and type
    echo '<p>File Size: ' . $file_size_human . '</p>';
    echo '<p>File Type: ' . $file_type . '</p>';
}

// Display attachment link
echo wp_get_attachment_link($attachment_id);
1
  • $file_type = $attachment_metadata['mime_type']; this for me not display a type of uploaded file
    – mardon
    Commented Mar 20 at 13:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.