且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在php中限制上传文件的大小

更新时间:2023-11-19 18:14:22

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

  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