简体   繁体   中英

How can I restrict the picture upload to a limited size? if more then the limited size then show a message?

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);               
  }

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM