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

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




  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值