We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 21112
    • 156 Posts
    I’m trying to use Ditto to replicate some of the functions of the FileDownload snippet, as it is not as flexible as I need. The only thing I need Ditto to do but haven’t been able to figure out, is display the file size of a file (doc or multimedia file etc) referenced via a web link. Is this possible?

    I initially tried using FileDownload for my purposes, but couldn’t get it to do everything I needed. Here’s the post I made in the FileDownload thread: http://modxcms.com/forums/index.php/topic,5419.msg126775.html#msg126775 I got this reply suggesting I could use a Ditto "extender" to get the file size information: http://modxcms.com/forums/index.php/topic,5419.msg127366.html#msg127366

    Thanks in advance for any help you can offer.
      • 7231
      • 4,205 Posts
      I don’t think that there is a way for ditto to get the file size information directly from the file (you may need a snippet for that). The recommendation in the last post you mentioned suggests that you simply use the link attribute field to include this to the ditto chunk (the extender to get Ditto to read the link attribute field). You could use any field or TV to manually insert the file size data and then display this in the Ditto chunk.

      To automatically get the file size check out the php function - http://www.php.net/filesize

      Here is an example:
      <?php
      
      // outputs e.g.  somefile.txt: 1024 bytes
      
      $filename = 'somefile.txt';
      echo $filename . ': ' . filesize($filename) . ' bytes';
      
      ?>
      You could use this in a snippet and feed the file path to get the file size then call this snippet within each ditto chunk. I have no idea about the overhead with this function so I can’t tell you if it will be heavy with huge file lists.
        [font=Verdana]Shane Sponagle | [wiki] Snippet Call Anatomy | MODx Developer Blog | [nettuts] Working With a Content Management Framework: MODx

        Something is happening here, but you don&#39;t know what it is.
        Do you, Mr. Jones? - [bob dylan]
        • 21112
        • 156 Posts
        Thanks for the suggestion! I’m not a php programmer by any stretch, so I’ll look to see if there’s any existing snippet to do such a thing, otherwise I’ll ask for some help in the snippet support forum.
          • 7231
          • 4,205 Posts
          This might work as long as you have the path handy (like in a TV for download):

          <?php
          
          $file = isset($file) ? $file : '';
          
          if ($file) {
          	return filesize($file) . ' bytes';
          } else {
          	return '';
          }	
          
          ?>


          and this would be an example of a call:
          [!fileSize? &file=`assets/files/index.html`!] 


          and it returns ’70 bytes’ where 70 would be the size of the file.

          Let me know what info you have available to identify the document. If you don’t have the path and only have the file name handy, we can hard code the path.
            [font=Verdana]Shane Sponagle | [wiki] Snippet Call Anatomy | MODx Developer Blog | [nettuts] Working With a Content Management Framework: MODx

            Something is happening here, but you don&#39;t know what it is.
            Do you, Mr. Jones? - [bob dylan]
            • 10449
            • 956 Posts
            // snippet fSize
            // example usage: [!fSize? &fn=`assets/images/austinpowers-01.jpg`!]
            
            $output = null;
            
            // from: http://ch2.php.net/manual/en/function.filesize.php#77518
            function size_readable ($size, $retstring = null) {
                    $sizes = array('B', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB');
                    if ($retstring === null) { $retstring = '%01.2f %s'; }
                    $lastsizestring = end($sizes);
                    foreach ($sizes as $sizestring) {
                            if ($size < 1024) { break; }
                            if ($sizestring != $lastsizestring) { $size /= 1024; }
                    }
                    if ($sizestring == $sizes[0]) { $retstring = '%01d %s'; } // Bytes aren't normally fractional
                    return sprintf($retstring, $size, $sizestring);
            }
            
            
            if($fn) {
            	if( !stristr($fn, '.htaccess') && !stristr($fn, '.php') && !stristr($fn, '.inc') && !stristr($fn, '.htm') ) {
            		$fullPath = $modx->config['base_path'] . $fn;
            		if(file_exists($fullPath)) {
            			$fsc = filesize($fullPath);
            			$fs = size_readable($fsc);
            			$baseURL = MODX_BASE_URL;
            			$shortFN = basename($fn);
            			$output = "<a href='$baseURL$fn' title='download $fn ($fs)'>$shortFN ($fs)</a><br />";
            			return($output);
            		}
            	}
            }
            
            



            This simple snippet checks if the file really exists, if it really is one of "allowed" file-type, and formats the filesize from bytes to KB/MB/GB if necessary, and output a simple link. That’s just one of many ways... you could also scan an entire directory and only list images etc. For single files however, this should work fine.