Comment obtenir la taille d'un fichier joint?
-
-
Faitintéressant,iln'y a aucunefonctionnalité dans lebackendpour afficher lataille d'unfichier que ce soit dans les détails ou dans la liste.[Ticket # 8739] (http://core.trac.wordpress.org/ticket/8739)Interestingly, there is no functionality in the backend to display the size of a file wether in details nor in the list. [Ticket #8739](http://core.trac.wordpress.org/ticket/8739)
- 0
- 2010-08-19
- hakre
-
7 réponses
- votes
-
-
Ah - ça a l'airbien mieux que dejouer avec `wp_upload_dir ()`etc.!Ah - that looks much better than messing around with `wp_upload_dir()` etc.!
- 0
- 2010-08-18
- Bobby Jack
-
J'aibesoin d'obtenir lataille dufichier d'une seulepiècejointe.j'ai utiliséget_the_ID () danspost_parent.maisinutile.i need to get only one post attachment's file size. i used get_the_ID() in post_parent. but no use.
- 0
- 2013-03-28
- KarSho
-
C'estbien,mais sachez que `filesize` lancera un avertissement si`get_attached_file` retournefalse.Vous devez ajouter une vérificationpour vous assurer que `get_attached_file` renvoie unfichier quiexiste.This is good, but be warned that `filesize` will throw a warning if `get_attached_file` returns false. You should add a check to ensure that `get_attached_file` returns a file that exists.
- 0
- 2019-12-07
- Alex Standiford
-
-
- 2010-08-19
J'ai déjà utilisé ceci dansfunctions.phppour afficher lataille dufichier dans unformatfacilement lisible:
function getSize($file){ $bytes = filesize($file); $s = array('b', 'Kb', 'Mb', 'Gb'); $e = floor(log($bytes)/log(1024)); return sprintf('%.2f '.$s[$e], ($bytes/pow(1024, floor($e))));}
Etpuis dansmonmodèle:
echo getSize('insert reference to file here');
I have used this before in functions.php to display the file size in an easily readable format:
function getSize($file){ $bytes = filesize($file); $s = array('b', 'Kb', 'Mb', 'Gb'); $e = floor(log($bytes)/log(1024)); return sprintf('%.2f '.$s[$e], ($bytes/pow(1024, floor($e))));}
And then in my template:
echo getSize('insert reference to file here');
-
Iln'estpasnécessaire de créer unenouvellefonction.WPen a deuxintégrés dans lenoyau.`size_format ()`et `wp_convert_bytes_to_hr ()`There is no need to create a new function. WP has two of them built into core. `size_format()` and `wp_convert_bytes_to_hr()`
- 8
- 2011-10-27
- Scott
-
On dirait que wp_convert_bytes_to_hr () [estmaintenant obsolète] (http://core.trac.wordpress.org/ticket/19067)en faveur de size_format ()Looks like wp_convert_bytes_to_hr() [has now been deprecated](http://core.trac.wordpress.org/ticket/19067) in favour of size_format()
- 8
- 2012-05-14
- davemac
-
- 2017-03-27
Jeferais:
$attachment_filesize = filesize( get_attached_file( $attachment_id ) );
Ou avec unetaille lisible comme
423.82 KB
$attachment_filesize = size_format( filesize( get_attached_file( $attachment_id ) ), 2 );
Réf: get_attached_file () , filesize () , size_format ()
Remarque: définissez votre
$attachment_id
I would do :
$attachment_filesize = filesize( get_attached_file( $attachment_id ) );
Or with readable size like
423.82 KB
$attachment_filesize = size_format( filesize( get_attached_file( $attachment_id ) ), 2 );
Refs : get_attached_file(), filesize(), size_format()
Note : Define your
$attachment_id
-
- 2017-01-17
Ilexiste une solutionplus simple,pour obtenir destailles defichiers lisiblespar l'homme.
$attachment_id = $attachment->ID; $attachment_meta = wp_prepare_attachment_for_js($attachment_id); echo $attachment_meta['filesizeHumanReadable'];
There's an easier solution, to get human readable file sizes.
$attachment_id = $attachment->ID; $attachment_meta = wp_prepare_attachment_for_js($attachment_id); echo $attachment_meta['filesizeHumanReadable'];
-
il y a unefonction wppourtout ;-)theres a wp_ funktion for everything ;-)
- 1
- 2017-11-06
- Thomas Fellinger
-
Devrait être la réponse acceptéeShould be the accepted answer
- 1
- 2019-01-16
- user1676224
-
- 2015-08-21
Pourtrouver lataille d'unfichier ajouté via leplugin de champspersonnalisés,j'aifait ceci:
$fileObject = get_field( 'file '); $fileSize = size_format( filesize( get_attached_file( $fileObject['id'] ) ) );
Assurez-vous simplement de définir la "Valeur de retour" du champpersonnalisé sur "Objet defichier".
To find the size of a file added through the custom fields plugin, I did this:
$fileObject = get_field( 'file '); $fileSize = size_format( filesize( get_attached_file( $fileObject['id'] ) ) );
Just make sure you set the custom field's "Return Value" to "File Object".
-
- 2011-07-05
Je cherchais lamême choseet j'aitrouvé cette solutionintégrée WordPress.
$args = array( 'post_type' => 'attachment', 'numberposts' => -1, 'post_status' => null, 'post_parent' => $main_post_id ); $attachments = get_posts($args); foreach ($attachments as $attachment) { $attachment_id = $attachment->ID; $image_metadata = wp_get_attachment_metadata( $attachment_id ); the_attachment_link($attachment->ID, false); echo the_attachment_link['width']; echo the_attachment_link['height']; }
En savoirplus sur
wp_get_attachment_metadata()
I was looking for the same and found this WordPress built-in solution.
$args = array( 'post_type' => 'attachment', 'numberposts' => -1, 'post_status' => null, 'post_parent' => $main_post_id ); $attachments = get_posts($args); foreach ($attachments as $attachment) { $attachment_id = $attachment->ID; $image_metadata = wp_get_attachment_metadata( $attachment_id ); the_attachment_link($attachment->ID, false); echo the_attachment_link['width']; echo the_attachment_link['height']; }
See more at
wp_get_attachment_metadata()
-
- 2017-05-27
Pour l'audio aumoins,lataille dufichierestenregistréeen tant que "métadonnées".
$metadata = wp_get_attachment_metadata( $attachment_id ); echo $metadata['filesize'];
Cela peutne pas être le caspour lesimageset les vidéos.
For audio at least, the file size is saved as "metadata".
$metadata = wp_get_attachment_metadata( $attachment_id ); echo $metadata['filesize'];
This may not be the case for images and video.
J'utilise le code demodèle suivantpour afficher les liens despiècesjointes:
mais après le lien,j'aibesoin d'afficher lataille dufichier.Commentpuis-jefaire cela?
Je suppose queje pourrais déterminer le chemin dufichier (via
wp_upload_dir()
et unsubstr()
dewp_get_attachment_url()
)etappelezfilesize()
mais cela semble compliqué,et jeme demande simplement s'ilexiste uneméthodeintégrée à WordPress.