View bytes as KB
Here is the script, I'll run you through it below.
PLEASE NOTE: This is an example database, you will need to customize this for it to actually work, please read my instructions below the script.
PHP Code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
<?
//Find if there is an id at the URL, eg. aussieferal.com/view.php?id=45
if(isset($_GET['id']))
{
$id = $_GET['id']; //$id = the url id... ?id=$id
$query = "SELECT name, size, FROM downloads WHERE id = '$id'"; //gets the info from the id provided in the url
$result = mysql_query($query) or die('Error, query failed');
ist($name, $size) = mysql_fetch_array($result);
//In order of the query, the info is assigned to a var. Like $name = name from the database query
//Otherwise, the ID is not in the url
}else{
die("ID doesn't exist");
exit(); //End the script
}
//Otherwise, everything is going well, create the function!
//KEEP IN MIND, THE VAR FOR THE SIZE IN THE QUERY IS $size
function convert_bytes( $bytes, $to=NULL )
{
$kb_echo= floatval( $bytes );
switch( $to )
{
case 'Kb' : // KB
$float = ( $float*8 ) / 1024;
break;
case 'b' : // B
$float *= 8;
break;
case 'MB' : // MB
$float /= 1024;
case 'KB' : // KB
$float /= 1024;
default : // B
}
unset( $bytes, $to );
return( $kb_echo );
}
$bytes = $size ;
//Now, the magic
$KB = convert_bytes( $bytes, 'KB' );
//We don't want 1024.336655333568 KB, we want 1,024.0 KB format, so we just use number_format(); to change it, simple, 'eh
$file_size = number_format( $KB, 1 );
echo "$file_size KB";
?>
Enjoy!