[wp-trac] [WordPress Trac] #47459: Introduce new a function to retrieve size of files

WordPress Trac noreply at wordpress.org
Sat Jun 1 23:44:47 UTC 2019


#47459: Introduce new a function to retrieve size of files
-----------------------------+------------------------------
 Reporter:  justinahinon     |       Owner:  (none)
     Type:  feature request  |      Status:  new
 Priority:  normal           |   Milestone:  Awaiting Review
Component:  Media            |     Version:
 Severity:  normal           |  Resolution:
 Keywords:                   |     Focuses:
-----------------------------+------------------------------
Changes (by SergeyBiryukov):

 * component:  General => Media


Comment:

 Hi @justinahinon, welcome to WordPress Trac! Thanks for the ticket.

 There is a [https://www.php.net/manual/en/function.filesize.php
 filesize()] PHP function that WordPress itself uses to
 [source:tags/5.2.1/src/wp-includes/media.php?marks=3269-3282#L3257 display
 the size of files in Media Library].

 You can use it to get the file size in bytes, e.g. `7358`:
 {{{
 $file = get_attached_file( $attachment_id );
 echo filesize( $file ); // size in bytes, e.g.
 }}}

 Or combine it with `size_format()` to get a human-readable string, e.g. `7
 KB`:
 {{{
 $file = get_attached_file( $attachment_id );
 echo size_format( filesize( $file ) );
 }}}

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/47459#comment:1>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list