0

I have as following but people are uploading profile pictures which is more then 1MB or 2MB and later it becomes very slow login/logout.

How can i limit it to maximum upload 150Kb? and if they upload more then 150Kb or trying to upload more then that size. show a message?

submit:

<form id="uploadForm" 
      action="<?php echo $fileupload;?>" 
      method="POST" 
      enctype="multipart/form-data" target="my_iframe">
  <input type="file" name="picture" id="picture" />
  <input type="hidden" name="MAX_FILE_SIZE" value="400000" />
  <input type="submit" name="submitprofile" id="submitprofile" 
               />
</form>

php:

  $fdata = 'empty';
  if (isset($_FILES['picture']) && $_FILES['picture']['size'] > 0) {
    $tmpName  = $_FILES['picture']['tmp_name'];  
    $fp      = fopen($tmpName, 'r');
    $fdata = fread($fp, filesize($tmpName));
    //$fdata = addslashes($fdata);
    $fdata = base64_encode($fdata);
    fclose($fp);
    //$sl = "INSERT INTO image (image)VALUES ( '$data')", $connection);               
  }
ZeroBased_IX
  • 2,667
  • 2
  • 25
  • 46

1 Answers1

1

Here you go:

$filesize = filesize($tmpName);
if ($filesize > (150 * 1024) || false == $filesize) { //filesize returns bytes or false 
    throw new InvalidArgumentException('Invalid file size');
}

Edit And here you can learn more about filesize function: http://php.net/manual/en/function.filesize.php

Radek Adamiec
  • 502
  • 3
  • 10