Function question

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
mrlayance
Forum Commoner
Posts: 31
Joined: Mon Dec 07, 2009 11:53 am

Function question

Post 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 />";
?> 
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: Function question

Post by Jonah Bron »

What doesn't work? Do you get an error?
User avatar
twinedev
Forum Regular
Posts: 984
Joined: Tue Sep 28, 2010 11:41 am
Location: Columbus, Ohio

Re: Function question

Post 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
Post Reply