Jump to content

File Size using functions


gurhy

Recommended Posts

Hi all

im new to this forum.

 

I hope in in the correct place to post this.

 

What I am trying to do is make a script that will print out a file size from bytes in to kb, mb. gb

 

but nothing seems to work properly as this is the error I get

 

6.8 MB

Fatal error: Cannot redeclare formatfilesize() (previously declared in /home/voyager/public_html/blocks/block-latest_50.php:44) in /home/voyager/public_html/blocks/block-latest_50.php on line 44

 

 

line 44 is this "function formatfilesize( $size ) {"

 

it needs to run through a loop then print out in side $content .="$size"'

 

I have attached the php file of this so you can see what I am trying to do as I have never worked with functions before so I have no clue what to do.

 

This php file is a block I am making for php-nuke that I run on my site, and there was no block like this what I wanted so I thought I make my own, but it is only this I am having problems with :(

 

[attachment deleted by admin]

Link to comment
Share on other sites

You cannot have a function declared within a while loop (or any loop for that matter). Every time the loop iterates, the function will be redeclared to PHP, which will obviously error. Place all functions at the bottom of your script, away from everything else. Call the function inside your loop with

 

<?php
while ( some_condition ) {
    // do stuff
    formatfilesize( $size );
    // more stuff

} // end loop

// rest of code

 

PhREEEk

Link to comment
Share on other sites

hi, I put that function at the bottom of the php file and now the error has gone and prints the sizes, although they are wrong but I sort that later

 

How do you print this formatfilesize( $size );  inside this $contents .="loop numbers in here";

 

this is where everything in the block has to be in order to display on the website correctly.

 

I am kinda asking alot, sorry, but do you think you could fix that bit of the code on the file I attached? it has to loop in the correct way.

 

if you go to this website http://eliteforce2.filefront.com/

 

loot towards the bottom you will find a simular thingk to what I am making

 

look for  " EF 2 Files' Latest 50 Files "

 

this is how it should print out.

 

you may of guessed from the file but this is so you can see a working version in action

Link to comment
Share on other sites

Please, this is the last bit I need doing for this script for now, so I can then activate for all my members on the site to see, rather than having the old plane version I have that came with the script,

 

If there is some way that will give me the exact same result I want that will print inside the $contect .=" ";

I may just take it, but has to work correctly and display the file size correctly

 

Link to comment
Share on other sites

Hi I am still trying to sort this but so far with no luck.

 

I would really love t have this script working but I just cant figure this out :S

 

 

<?php

if ( !defined('BLOCK_FILE') ) {
    Header("Location: ../index.php");
    die();
}

global $prefix, $db;

$content .= "
<style type=\"text/css\">
<!--
.style1 {
font-family: Arial, Helvetica, sans-serif;
font-weight: bold;
}
.style2 {font-family: Arial, Helvetica, sans-serif}
.style37 {font-family: Arial, Helvetica, sans-serif; font-size: 14px; }
-->
</style>
<table width='100%' cellpadding='0' cellspacing='0'>
  <TR height='20'>
    <TD align='left'><span class='style1'>Game</span></TD>
    <TD width='60%' align='left'><span class='style1'>Name</span></TD>
    <TD width='1%' align='left'><span class='style1'>Q</span></TD>
    <TD align='center'><span class='style1'>Size</span></TD>
    <TD width='1%' align='left'><span class='style2'></span></TD>
    <TD align='center'><span class='style2'><strong>Category</strong></span></TD>
  </TR>  <TR>";
      
$result = $db->sql_query("SELECT lid, title, game, category, totalcomments, date, filesize FROM ".$prefix."_downloads_downloads ORDER BY lid DESC");
    	

while ($row = $db->sql_fetchrow($result)) {
    $lid = intval($row['lid']);
    $title = filter($row['title'], "nohtml");
    $title2 = ereg_replace("_", " ", $title);
    $game = ($row['game']);
    $category = ($row['category']);
    $date = ($row['date']);
    $sizez = ($row['filesize']);
 $size += $row[ "filesize" ];
    $totalcomments = ($row['totalcomments']);



$content .= ". print_r(formatfilesize($size)); ."
    <TD align='left'>
<span class='style37'>$game</span></TD>
    <TD align='left'>
<span class='style37'>
<A title='$title2' href='modules.php?name=Downloads&d_op=viewdownloaddetails&lid=$lid&title=$title'>$title2</A></span></TD>
    <TD align='left'>
<span class='style37'>$totalcomments</span></TD>
    <TD align='right'>
<span class='style37'>";



$content .= "
</span></TD>
    <TD align='right'> </TD>
    <TD align='center'>
<span class='style37'>$category</span></TD>
  </TR>";
}    
$content .= "</table>
";

function formatfilesize( $size ) {
        // bytes
        if( $size < 1024 ) { echo $size . " bytes";                }

        // kilobytes
        else if( $size < 1024000 ) { echo round( ( $size / 1024 ), 1 ) . "k";                }

        // megabytes
        else { echo  round( ( $size / 1024000 ), 1 ) . " MB";        }}
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.