【问题标题】:How can I restrict the picture upload to a limited size? if more then the limited size then show a message?如何将图片上传限制为有限大小?如果超过限制的大小,然后显示一条消息?
【发布时间】:2016-05-08 23:57:42
【问题描述】:

我有以下内容,但人们正在上传超过 1MB 或 2MB 的个人资料图片,后来登录/注销变得非常缓慢。

如何将其限制为最大上传 150Kb?如果他们上传超过 150Kb 或尝试上传超过该大小。显示消息?

提交:

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

【问题讨论】:

标签: javascript php html google-chrome


【解决方案1】:

给你:

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

编辑 在这里您可以了解有关文件大小功能的更多信息: http://php.net/manual/en/function.filesize.php

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-13
    • 1970-01-01
    • 1970-01-01
    • 2011-10-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多