Jump to content

Which data type to store filesize?


abdulqadir

Recommended Posts

I've made a upload script which uploads files upto 1GB and it works like this. It takes the values of  name,size, type from the $_FILES function and stores it to one of the tables of mysql. The problem is that the value which stores in the "size" column is always 9999.99 irrespective of the filesize. I've choosen decimal (6,2) as datatype for the size column.

 

Note: I've divided the $_FILE['file']['size'] by 1048576 to convert the filesize from Bytes to Mega Bytes.

 

Here's my upload code though i'm pretty much sure that the upload code is fine. The problem is with the datatype.

 

<?php
$uploaddir = "d:/server_uploads/";
if((isset($_POST['upload'])) && (($_FILES['file']['type']== "video/x-matroska") || ($_FILES['file']['type']== "video/mpeg") || ($_FILES['file']['type']== "video/avi")|| ($_FILES['file']['type']== "video/vnd.rn-realvideo") || ($_FILES['file']['type']== "video/vnd.rn-realvideo")|| ($_FILES['file']['type']== "application/octet-stream")|| ($_FILES['file']['type']== "audio/mpeg")) && ($_FILES["file"]["size"] > 1048576))  
{
$fileName = $_FILES['file']['name'];
$tmpName = $_FILES['file']['tmp_name'];
$fileSize = $_FILES['file']['size'];
$fileType = $_FILES['file']['type'];

$filePath = $uploaddir . $fileName;

$result = move_uploaded_file($tmpName, $filePath);

if (!$result) 
{
	echo "Error uploading file";
	exit;
}

echo "Your file has been uploaded succesfully!! Below are details: <br />";
echo "File: ". $_FILES['file']['name'] . " <br />";
echo "Type: ". $_FILES['file']['type'] . " <br />";
echo "Size: ". ($_FILES['file']['size'] / 1048576) . " MB<br />";

}
else {
echo "Filtype is not supported";
exit;
}
$con = mysql_connect ("localhost:3306", "root", "");
mysql_select_db ("server", $con);

if(!get_magic_quotes_gpc())
{
$fileName = addslashes($fileName);
$filePath = addslashes($filePath);
}

if ($fileName!="") 
{
$query = "INSERT INTO uploads (name, type, size, path ) " . "VALUES ('$fileName', '$fileType', '$fileSize', '$fileName')";

mysql_query($query);

}

mysql_close($con);

?>

 

 

Link to comment
https://forums.phpfreaks.com/topic/219725-which-data-type-to-store-filesize/
Share on other sites

as jdavidbakr mentioned above, you're not dividing your final variable before it goes into the database. You don't really have to, though. Just use a BIGINT UNSIGNED, which will hold values from 0 to 18446744073709551615, and store the size in bytes. You also won't need a decimal digit in a field that stores the filesize. You can read about the mySQL numeric types here: http://dev.mysql.com/doc/refman/5.0/en/numeric-type-overview.html

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.