phpmysqlfunctionfilesizeredeclare

PHP MySQL - Need to display list of files and their file size, formatted. Redeclare function error


I'm stuck on something that's probably simple for more experienced PHP folks. I'm getting a "cannot redeclare function" error when I try to format the file size of a list of files I'm querying a mysql database for.

From what I can tell, I shouldn't put the function inside the while statement, but if I don't, the $bytes variable won't change for each file that's called from the database. Can someone explain how I can make this work?

Here's the code:

while ($docRows = mysql_fetch_array($docsQuery)) {

 $docID = $docRows['ID'];
 $docName = htmlspecialchars($docRows['docName']);
 $docDescription = htmlspecialchars($docRows['LEFT(docDescription, 50)']);
 $docPath = $docRows['filePath'];
 $origName = $docRows['origName'];
 $bytes = filesize($_SERVER['DOCUMENT_ROOT']."/documents/".$origName."");

 function bytesToSize($bytes, $precision = 2) {  
$kilobyte = 1024;
$megabyte = $kilobyte * 1024;
$gigabyte = $megabyte * 1024;
$terabyte = $gigabyte * 1024;

if (($bytes >= 0) && ($bytes < $kilobyte)) {
    return $bytes . ' B';

} elseif (($bytes >= $kilobyte) && ($bytes < $megabyte)) {
    return round($bytes / $kilobyte, $precision) . ' KB';

} elseif (($bytes >= $megabyte) && ($bytes < $gigabyte)) {
    return round($bytes / $megabyte, $precision) . ' MB';

} elseif (($bytes >= $gigabyte) && ($bytes < $terabyte)) {
    return round($bytes / $gigabyte, $precision) . ' GB';

} elseif ($bytes >= $terabyte) {
    return round($bytes / $terabyte, $precision) . ' TB';
} else {
    return $bytes . ' B';
}
}

echo ('<tr>
    <td><a href="'.$docPath.'">'.$docName.'</a> ('.bytesToSize($bytes).')</td>
    <td>');

    if (!empty($docDescription)) {
    echo(''.$docDescription.'...');
    }
 else {
    // don't show description if there isn't one
    }

echo ('</td>
</tr>
');

} // end while

Solution

  • You need to define your function then call it, if you define a function more than once PHP will throw an error, that is why you should not define your function in a loop.

    regarding echo it does not require parenthesis, please take a look at the below code:

    function bytesToSize($bytes, $precision = 2) {  
        $kilobyte = 1024;
        $megabyte = $kilobyte * 1024;
        $gigabyte = $megabyte * 1024;
        $terabyte = $gigabyte * 1024;
    
        if (($bytes >= 0) && ($bytes < $kilobyte)) {
            return $bytes . ' B';
    
        } elseif (($bytes >= $kilobyte) && ($bytes < $megabyte)) {
            return round($bytes / $kilobyte, $precision) . ' KB';
    
        } elseif (($bytes >= $megabyte) && ($bytes < $gigabyte)) {
            return round($bytes / $megabyte, $precision) . ' MB';
    
        } elseif (($bytes >= $gigabyte) && ($bytes < $terabyte)) {
            return round($bytes / $gigabyte, $precision) . ' GB';
    
        } elseif ($bytes >= $terabyte) {
            return round($bytes / $terabyte, $precision) . ' TB';
        } else {
            return $bytes . ' B';
        }
    }
    
    while ($docRows = mysql_fetch_array($docsQuery)) {
         $docID = $docRows['ID'];
         $docName = htmlspecialchars($docRows['docName']);
         $docDescription = htmlspecialchars($docRows['LEFT(docDescription, 50)']);
         $docPath = $docRows['filePath'];
         $origName = $docRows['origName'];
         $bytes = filesize($_SERVER['DOCUMENT_ROOT']."/documents/".$origName."");
    
        echo '<tr>
            <td><a href="'.$docPath.'">'.$docName.'</a> ('.bytesToSize($bytes).')</td>
            <td>';
    
        if (!empty($docDescription)) {
            echo(''.$docDescription.'...');
        }
         else {
        // don't show description if there isn't one
        }
    
        echo '</td>
            </tr>
        ';
    
    } // end while