PHP上传大文件之前检查文件大小方法
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
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 } }
声明:该文观点仅代表作者本人,入门客AI创业平台信息发布平台仅提供信息存储空间服务,如有疑问请联系rumenke@qq.com。
- 上一篇:没有了
- 下一篇: YII2-数据库数据查询方法,关联查询with, joinWith区别和分页