1.在客户端检查
<form method="post" enctype="multipart/form-data" action="upload.php">
<input type="file" name="file" id="file" />
<input type="submit" name="submit" value="Submit" />
</form>
<script>
document.forms[0].addEventListener('submit', function( evt ) {
var file = document.getElementById('file').files[0];
if(file && file.size < 10485760) { // 10 MB (this size is in bytes)
//Submit form
}
else {
//Prevent default and display error
evt.preventDefault();
}
}, false);
</script>
2.在服务器检查
在 /php.x.xx/php.ini 或 应用程序的 .htaccess中配置, 这个缺点是上传操作一旦提交,不可自动撤消直到上传的文件大小超出配置值UPLOAD_MAX_FILESIZE = 100M
MAX_FILE_SIZE = 100M
max_execution_time = 60 (default 60)
memory_limit = 200M
max_input_time = 2 (default 60)
在项目中设置.htaccess
UPLOAD_MAX_FILESIZE = 100M
MAX_FILE_SIZE = 100M
max_execution_time = 60
max_input_time = 2 #2够用
3.在PHP脚本检查
在服务端脚本检查,不适用即时检查upload.php
<?php
if(isset($_FILES['file']) {
if($_FILES['file']['size'] > 10485760) { //10 MB (size is also in bytes)
// File too big
}
else {
// File within size restrictions
}
}