Page 1 of 1

Function question

Posted: Fri Oct 08, 2010 12:10 pm
by mrlayance
Well, more of a realization. I don't get functions... This is not working, I am trying to get file size.

Code: Select all

<?php 
  
function format_size($file) {
      $sizes = array(" Bytes", " KB", " MB", " GB", " TB", " PB", " EB", " ZB", " YB");
      if ($file == 0) { return('n/a'); } else {
      return (round($file/pow(1024, ($i = floor(log($file, 1024)))), 2) . $sizes[$i]); }
}

  $current_dir = "/export/home/ftp";    //Put in second part, the directory - without a leading slash but with a trailing slash! 
  $dir = opendir($current_dir);        // Open the sucker 
  
  echo ("<p><h2>Workstation Software Updates</h2></p><br />"); 
  while ($file = readdir($dir))            // while loop 
    { 
    $parts = explode(".", $file);                    // pull apart the name and dissect by period 
    if (is_array($parts) && count($parts) > 1) {    // does the dissected array have more than one part 
        $extension = end($parts);        // set to we can see last file extension 
        if ($extension == "zip" OR $extension == "ZIP")    // is extension ext or EXT ? 
			 echo "<li><a href=\"$file\" target=\"_blank\">$file - format_size()</a></li><br />";    // If so, echo it out else do nothing cos it's not what we want 
			 echo "format_size()";
		} 
    } 
  echo "<br />";
?> 

Re: Function question

Posted: Fri Oct 08, 2010 12:58 pm
by Jonah Bron
What doesn't work? Do you get an error?

Re: Function question

Posted: Fri Oct 08, 2010 1:13 pm
by twinedev
You have the function name inside of quotes in an echo statement, and on top of that, you are not passing it the size of the file...

Code: Select all

echo "Stuff to directly output  $andSomeVariable " , format_size(filesize($dir.'/'.$file)), " rest of string to echo ";
-Greg