user1832425
user1832425

Reputation: 27

Use a different measurement of data rather than KB

I'm currently tinkering around with some code which allows me to upload files to my site. I am going to set an option in a configuration file which allows users to set the maximum upload limit. At the moment this has to be entered in kilobytes and I was wondering whether or not it'd be possible to have it entered in MB?

if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/png")
|| ($_FILES["file"]["type"] == "image/pjpeg"))
**&& ($_FILES["file"]["size"] < 2000000)** <---- Entered in Kilobytes, not MB.
&& in_array($extension, $allowedExts))

Upvotes: 0

Views: 84

Answers (3)

Prasanth
Prasanth

Reputation: 5268

<?php
    define('MB', 1024*1024);
    echo 10*MB;
?>

That's another way to do it.

Also, there are no macros in C. So, it's not possible like in C,C++ to #define these things.

Upvotes: 0

Michael Oliver
Michael Oliver

Reputation: 1412

It may be best to define a function to convert to MB to bytes, i.e.:

function MB($mb) {
   return $mb*1024*1024;
}

Then you can make your comparison like:

($_FILES["file"]["size"] < MB(2000))

Upvotes: 0

David Harris
David Harris

Reputation: 2707

Just do this:

$b = 1000000;
$kb = $b / 1024;
$mb = $kb / 1024;
if($_FILES['size']['size'] < $mb * 2) {
    // do something;
}

Upvotes: 1

Related Questions