我有以下内容,但人们正在上传超过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);
}
答案 0 :(得分:2)
你走了:
$filesize = filesize($tmpName);
if ($filesize > (150 * 1024) || false == $filesize) { //filesize returns bytes or false
throw new InvalidArgumentException('Invalid file size');
}
修改强> 在这里,您可以了解有关filesize函数的更多信息: http://php.net/manual/en/function.filesize.php