{"id":360,"date":"2011-05-03T10:37:08","date_gmt":"2011-05-03T09:37:08","guid":{"rendered":"http:\/\/www.manuelmarangoni.it\/sir-bit\/?p=360"},"modified":"2016-01-08T16:43:56","modified_gmt":"2016-01-08T15:43:56","slug":"php-mostrare-la-dimensione-di-una-cartella","status":"publish","type":"post","link":"https:\/\/www.manuelmarangoni.it\/sir-bit\/360\/php-mostrare-la-dimensione-di-una-cartella\/","title":{"rendered":"PHP &#8211; Mostrare la dimensione di una cartella"},"content":{"rendered":"<p>Se avete un&#8217;area di gestione file, o comunque avete bisogno di mostrare la dimensione occupata in una determinata cartella, sottocartelle incluse, lo script che segue potrebbe fare per voi.<\/p>\n<p>Il codice tiene conto dei DIRECTORY_SEPARATOR e non si limita ai file della cartella selezionata: <strong>scorre tutti i file delle cartelle annidiate<\/strong>.<\/p>\n<p>Andiamo a vederlo.<!--more--><\/p>\n\n        <h2>Download script<\/h2>\n        <div style=\"float:left; width:100%; margin-top:60px; margin-bottom:40px;\">\n        <div style=\"float:left; width:100px;\"><img decoding=\"async\" title=\"[PHP] Mostrare dimensioni di una cartella\" src=\"https:\/\/www.manuelmarangoni.it\/sir-bit\/wp-content\/themes\/sirbit\/images\/redirect_icon.png\" alt=\"[PHP] Mostrare dimensioni di una cartella\" \/><\/div>\n        <div style=\"float:left; width:350px; padding-top:30px; font-size:14px;\"><strong><a title=\"[PHP] Mostrare dimensioni di una cartella\" href=\"https:\/\/www.manuelmarangoni.it\/sir-bit\/code\/php-mostrare-dimensioni-di-una-cartella\/\">Pagina diretta alla scheda e al download del codice<\/a><\/strong><\/div>\n        <\/div>\n        <div style=\"clear:both;\"> <\/div>\n<h2>Recuperare le dimensioni di una cartella e delle sottocartelle &#8211; La funzione nel dettaglio<\/h2>\n<p>Per comodit\u00e0 ho inserito i commenti nel codice stesso, che dovrebbe risultare sufficientemente comprensibile.<\/p>\n<div class=\"wp_syntax\">\n<table>\n<tbody>\n<tr>\n<td class=\"line_numbers\">\n<pre>1\r\n2\r\n3\r\n4\r\n5\r\n6\r\n7\r\n8\r\n9\r\n10\r\n11\r\n12\r\n13\r\n14\r\n15\r\n16\r\n17\r\n18\r\n19\r\n20\r\n21\r\n22\r\n23\r\n24\r\n25\r\n26\r\n27\r\n28\r\n29\r\n30<\/pre>\n<\/td>\n<td class=\"code\">\n<pre class=\"php\" style=\"font-family: monospace;\"><span style=\"color: #000000; font-weight: bold;\">function<\/span> foldersize<span style=\"color: #009900;\">(<\/span><span style=\"color: #000088;\">$dirname<\/span><span style=\"color: #009900;\">)<\/span> <span style=\"color: #009900;\">{<\/span>\t\r\n    <span style=\"color: #666666; font-style: italic;\">\/\/ se la cartella non esiste o non ha i permessi di lettura, ritorno false<\/span>\r\n    <span style=\"color: #b1b100;\">if<\/span> <span style=\"color: #009900;\">(<\/span><span style=\"color: #339933;\">!<\/span><span style=\"color: #990000;\">is_dir<\/span><span style=\"color: #009900;\">(<\/span><span style=\"color: #000088;\">$dirname<\/span><span style=\"color: #009900;\">)<\/span> <span style=\"color: #339933;\">||<\/span> <span style=\"color: #339933;\">!<\/span><span style=\"color: #990000;\">is_readable<\/span><span style=\"color: #009900;\">(<\/span><span style=\"color: #000088;\">$dirname<\/span><span style=\"color: #009900;\">)<\/span><span style=\"color: #009900;\">)<\/span>\r\n        <span style=\"color: #b1b100;\">return<\/span> <span style=\"color: #009900; font-weight: bold;\">false<\/span><span style=\"color: #339933;\">;<\/span>\r\n\r\n    <span style=\"color: #666666; font-style: italic;\">\/\/ immagazzino la cartella nell'array<\/span>\r\n    <span style=\"color: #000088;\">$dirname_stack<\/span><span style=\"color: #009900;\">[<\/span><span style=\"color: #009900;\">]<\/span> <span style=\"color: #339933;\">=<\/span> <span style=\"color: #000088;\">$dirname<\/span><span style=\"color: #339933;\">;<\/span>\r\n    <span style=\"color: #000088;\">$size<\/span> <span style=\"color: #339933;\">=<\/span> <span style=\"color: #cc66cc;\">0<\/span><span style=\"color: #339933;\">;<\/span>\r\n\r\n    <span style=\"color: #b1b100;\">do<\/span> <span style=\"color: #009900;\">{<\/span>\r\n\t<span style=\"color: #666666; font-style: italic;\">\/\/ assegno a $dirname il primo elemento di $dirname_stack e lo tolgo dall'array<\/span>\r\n        <span style=\"color: #000088;\">$dirname<\/span> <span style=\"color: #339933;\">=<\/span> <span style=\"color: #990000;\">array_shift<\/span><span style=\"color: #009900;\">(<\/span><span style=\"color: #000088;\">$dirname_stack<\/span><span style=\"color: #009900;\">)<\/span><span style=\"color: #339933;\">;<\/span>\r\n        <span style=\"color: #000088;\">$handle<\/span> <span style=\"color: #339933;\">=<\/span> <span style=\"color: #990000;\">opendir<\/span><span style=\"color: #009900;\">(<\/span><span style=\"color: #000088;\">$dirname<\/span><span style=\"color: #009900;\">)<\/span><span style=\"color: #339933;\">;<\/span>\r\n\t<span style=\"color: #666666; font-style: italic;\">\/\/ scorro tutti i file presenti<\/span>\r\n        <span style=\"color: #b1b100;\">while<\/span> <span style=\"color: #009900;\">(<\/span><span style=\"color: #009900; font-weight: bold;\">false<\/span> <span style=\"color: #339933;\">!==<\/span> <span style=\"color: #009900;\">(<\/span><span style=\"color: #000088;\">$file<\/span> <span style=\"color: #339933;\">=<\/span> <span style=\"color: #990000;\">readdir<\/span><span style=\"color: #009900;\">(<\/span><span style=\"color: #000088;\">$handle<\/span><span style=\"color: #009900;\">)<\/span><span style=\"color: #009900;\">)<\/span><span style=\"color: #009900;\">)<\/span> <span style=\"color: #009900;\">{<\/span>\r\n            <span style=\"color: #b1b100;\">if<\/span> <span style=\"color: #009900;\">(<\/span><span style=\"color: #000088;\">$file<\/span> <span style=\"color: #339933;\">!=<\/span> <span style=\"color: #0000ff;\">'.'<\/span> <span style=\"color: #339933;\">&amp;&amp;<\/span> <span style=\"color: #000088;\">$file<\/span> <span style=\"color: #339933;\">!=<\/span> <span style=\"color: #0000ff;\">'..'<\/span> <span style=\"color: #339933;\">&amp;&amp;<\/span> <span style=\"color: #990000;\">is_readable<\/span><span style=\"color: #009900;\">(<\/span><span style=\"color: #000088;\">$dirname<\/span> <span style=\"color: #339933;\">.<\/span> <span style=\"color: #009900; font-weight: bold;\">DIRECTORY_SEPARATOR<\/span> <span style=\"color: #339933;\">.<\/span> <span style=\"color: #000088;\">$file<\/span><span style=\"color: #009900;\">)<\/span><span style=\"color: #009900;\">)<\/span> <span style=\"color: #009900;\">{<\/span>\r\n                <span style=\"color: #b1b100;\">if<\/span> <span style=\"color: #009900;\">(<\/span><span style=\"color: #990000;\">is_dir<\/span><span style=\"color: #009900;\">(<\/span><span style=\"color: #000088;\">$dirname<\/span> <span style=\"color: #339933;\">.<\/span> <span style=\"color: #009900; font-weight: bold;\">DIRECTORY_SEPARATOR<\/span> <span style=\"color: #339933;\">.<\/span> <span style=\"color: #000088;\">$file<\/span><span style=\"color: #009900;\">)<\/span><span style=\"color: #009900;\">)<\/span> <span style=\"color: #009900;\">{<\/span>\r\n\t\t    <span style=\"color: #666666; font-style: italic;\">\/\/ inserisco nell'array il nome e l'indirizzo del file<\/span>\r\n                    <span style=\"color: #000088;\">$dirname_stack<\/span><span style=\"color: #009900;\">[<\/span><span style=\"color: #009900;\">]<\/span> <span style=\"color: #339933;\">=<\/span> <span style=\"color: #000088;\">$dirname<\/span> <span style=\"color: #339933;\">.<\/span> <span style=\"color: #009900; font-weight: bold;\">DIRECTORY_SEPARATOR<\/span> <span style=\"color: #339933;\">.<\/span> <span style=\"color: #000088;\">$file<\/span><span style=\"color: #339933;\">;<\/span>\r\n                <span style=\"color: #009900;\">}<\/span>\r\n\t\t<span style=\"color: #666666; font-style: italic;\">\/\/ aggiunge la dimensione di ogni file<\/span>\r\n                <span style=\"color: #000088;\">$size<\/span> <span style=\"color: #339933;\">+=<\/span> <span style=\"color: #990000;\">filesize<\/span><span style=\"color: #009900;\">(<\/span><span style=\"color: #000088;\">$dirname<\/span> <span style=\"color: #339933;\">.<\/span> <span style=\"color: #009900; font-weight: bold;\">DIRECTORY_SEPARATOR<\/span> <span style=\"color: #339933;\">.<\/span> <span style=\"color: #000088;\">$file<\/span><span style=\"color: #009900;\">)<\/span><span style=\"color: #339933;\">;<\/span>\r\n            <span style=\"color: #009900;\">}<\/span>\r\n        <span style=\"color: #009900;\">}<\/span>\r\n\t<span style=\"color: #666666; font-style: italic;\">\/\/ chiude la directory<\/span>\r\n        <span style=\"color: #990000;\">closedir<\/span><span style=\"color: #009900;\">(<\/span><span style=\"color: #000088;\">$handle<\/span><span style=\"color: #009900;\">)<\/span><span style=\"color: #339933;\">;<\/span>\r\n    <span style=\"color: #009900;\">}<\/span> <span style=\"color: #b1b100;\">while<\/span> <span style=\"color: #009900;\">(<\/span><span style=\"color: #990000;\">count<\/span><span style=\"color: #009900;\">(<\/span><span style=\"color: #000088;\">$dirname_stack<\/span><span style=\"color: #009900;\">)<\/span> <span style=\"color: #339933;\">&gt;<\/span> <span style=\"color: #cc66cc;\">0<\/span><span style=\"color: #009900;\">)<\/span><span style=\"color: #339933;\">;<\/span>\r\n\r\n    <span style=\"color: #b1b100;\">return<\/span> <span style=\"color: #000088;\">$size<\/span><span style=\"color: #339933;\">;<\/span>\r\n<span style=\"color: #009900;\">}<\/span><\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<p>Il concetto dello script \u00e8 che vengono inserite in un array le cartelle da scorrere, e con <a href=\"http:\/\/php.net\/manual\/en\/function.array-shift.php\" target=\"_blank\">array_shift<\/a> l&#8217;indirizzo viene associato alla variabile <code>$dirname<\/code> e sottratto dall&#8217;array.<\/p>\n\n<p>In questo modo il ciclo si ferma quando l&#8217;array risulta vuoto, cio\u00e8 sono state lavorate tutte le cartelle o sottocartelle.<\/p>\n<h2>Trasformare le dimensioni da byte a megabyte<\/h2>\n<p><code>Filesize<\/code> restituisce il valore in byte, quindi <strong>per mostrare i kilobyte si deve moltiplicare per 1024<\/strong>.<br \/>\nPer mostrare i megabyte, si deve moltiplicare per 1024*1024.<\/p>\n<p>Se dovessi leggere il contenuto della cartella &#8220;file&#8221; e mostrare i megabyte ottenuti, si procede cos\u00ec<\/p>\n<div class=\"wp_syntax\">\n<table>\n<tbody>\n<tr>\n<td class=\"line_numbers\">\n<pre>1\r\n2<\/pre>\n<\/td>\n<td class=\"code\">\n<pre class=\"php\" style=\"font-family: monospace;\"><span style=\"color: #000088;\">$mb_occupati<\/span><span style=\"color: #339933;\">=<\/span>foldersize<span style=\"color: #009900;\">(<\/span><span style=\"color: #0000ff;\">\"file\"<\/span><span style=\"color: #009900;\">)<\/span><span style=\"color: #339933;\">\/<\/span><span style=\"color: #009900;\">(<\/span><span style=\"color: #cc66cc;\">1024<\/span><span style=\"color: #339933;\">*<\/span><span style=\"color: #cc66cc;\">1024<\/span><span style=\"color: #009900;\">)<\/span><span style=\"color: #339933;\">;<\/span>\r\n<span style=\"color: #b1b100;\">echo<\/span> <span style=\"color: #990000;\">ceil<\/span><span style=\"color: #009900;\">(<\/span><span style=\"color: #009900;\">(<\/span><span style=\"color: #000088;\">$mb_occupati<\/span><span style=\"color: #009900;\">)<\/span><span style=\"color: #009900;\">)<\/span><span style=\"color: #339933;\">.<\/span><span style=\"color: #0000ff;\">\" MB\"<\/span><span style=\"color: #339933;\">;<\/span><\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<p>In questo modo mostro i <strong>megabyte arrotondati per eccesso<\/strong> (per evitare tutte le virgole dei byte).<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Se avete un&#8217;area di gestione file, o comunque avete bisogno di mostrare la dimensione occupata in una determinata cartella, sottocartelle incluse, lo script che segue potrebbe fare per voi. Il codice tiene conto dei DIRECTORY_SEPARATOR e non si limita ai file della cartella selezionata: scorre tutti i file delle cartelle annidiate. Andiamo a vederlo.<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[78],"tags":[31,4,6],"class_list":["post-360","post","type-post","status-publish","format-standard","hentry","category-php-mysql","tag-file","tag-php","tag-script","cat-78-id"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.manuelmarangoni.it\/sir-bit\/wp-json\/wp\/v2\/posts\/360"}],"collection":[{"href":"https:\/\/www.manuelmarangoni.it\/sir-bit\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.manuelmarangoni.it\/sir-bit\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.manuelmarangoni.it\/sir-bit\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.manuelmarangoni.it\/sir-bit\/wp-json\/wp\/v2\/comments?post=360"}],"version-history":[{"count":12,"href":"https:\/\/www.manuelmarangoni.it\/sir-bit\/wp-json\/wp\/v2\/posts\/360\/revisions"}],"predecessor-version":[{"id":2173,"href":"https:\/\/www.manuelmarangoni.it\/sir-bit\/wp-json\/wp\/v2\/posts\/360\/revisions\/2173"}],"wp:attachment":[{"href":"https:\/\/www.manuelmarangoni.it\/sir-bit\/wp-json\/wp\/v2\/media?parent=360"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.manuelmarangoni.it\/sir-bit\/wp-json\/wp\/v2\/categories?post=360"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.manuelmarangoni.it\/sir-bit\/wp-json\/wp\/v2\/tags?post=360"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}