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

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.