在php中限制上传文件的大小 [英] restricting the uploading files size in php

查看:163
本文介绍了在php中限制上传文件的大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用PHP上传多个文件,我还使用下面的HTML命令在上传表单PHP文件中修复了10MB的上传限制:

 < input type =hiddenname =MAX_FILE_SIZEvalue =10000000> 
< input id =infiletype =filename =infile []multiple =true/>

在负责上传功能的PHP文件中,我最初期待如果我尝试上传文件的大小大于10MB,那么函数调用语句

  move_uploaded_file($ _ FILES ['infile'] ['tmp_name'] [ $ i],$ dir。$ fPath); 

会失败,我可以显示一个小于10MB的错误上传文件消息。但是没有发生。它试图上传,并没有按预期显示任何错误消息。



所以我试图通过使用if语句来特别限制代码中的文件大小:

<$ p $ ($ _FILES [infile] [size] [$ i]< 10000000)
{
move_uploaded_file($ _ FILES ['infile'] ['' tmp_name'] [$ i],$ dir。$ fPath);
}
else
echoerror;

但是它仍然没有像预期的那样回显错误。任何人都可以指出我在这里做的错误吗?

解决方案

您可以将以下行添加到处理您的目标脚本表单:

  ini_set('upload_max_filesize','10M'); 

或者,如果您可以访问您的php.ini,只需更改以下内容:

  upload_max_filesize = 10M 

手册页: http://php.net/manual/en/ini.core.php


I am working with multiple file upload in PHP and I also fixed an upload limit of 10MB using the following HTML commands in an upload form PHP file:

   <input type="hidden" name="MAX_FILE_SIZE" value="10000000"> 
   <input id="infile" type="file" name="infile[]" multiple="true" />

In the PHP file that takes care of upload function I was initially expecting that if I try to upload a file of size greater than 10MB then the function call statement

   move_uploaded_file($_FILES['infile']['tmp_name'][$i], $dir . $fPath);

will fail and I can show an "Error upload file of size less than 10MB" message. But it didnt happen. It was trying to upload and it didnt display any error message as expected.

So I tried to restrict the file size specifically in the code by using the if statement as:

  if ($_FILES["infile"]["size"][$i]<10000000) 
    {
       move_uploaded_file($_FILES['infile']['tmp_name'][$i], $dir . $fPath);
    }
  else
    echo "error";

But still it doesnt echo error as expected. Can anyone please point out the mistake I am doing here?

解决方案

You can add the following line to your target script that handle your form:

ini_set('upload_max_filesize', '10M');

Or if you can access your php.ini, just change the following :

upload_max_filesize = 10M

Manual page : http://php.net/manual/en/ini.core.php

这篇关于在php中限制上传文件的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆