如何在php(而不是php.ini)中设置最大上传大小值?

时间:2016-08-04 16:08:59

标签: php

我试图将大小限制为50字节只是为了测试它,但这种方法由于某种原因不起作用。我无法访问php.ini,如果可能的话,我希望用代码执行此操作。

表格:

 <form action='uploadFile.php' target='uploadIframe' method='post' enctype='multipart/form-data'>
<div class='fieldRow'>
 <label>Select the file: </label>
 <input type='file' name='file' id='file' onChange='submitForm1(this)' />

 </div>
    </form>
<iframe style='border:0;' id='uploadIframe' name='uploadIframe'></iframe>
  <div id='successMessage'></div>

uploadFile.php

<!doctype html>
<html lang="en">
<head>
 <script src="//code.jquery.com/jquery-1.10.2.js"></script>
 </head>
 <body>
 <?php
 $upload_dir = "../sitedata/auditfiles";
 $result["status"] = "200";
 $result["message"]= "Error!";

 if(isset($_FILES['file'])){

    echo "Uploading file... <br />";

     if ($_FILES['file']['error'] == UPLOAD_ERR_OK) {
          $filename = $_FILES['file']['name'];
          move_uploaded_file($_FILES['file']['tmp_name'],
          $upload_dir.'/'.$filename);
           $result["status"] = "100";
          $result["message"]="File was uploaded successfully!";

问题在于:

      }elseif ($_FILES["file"]["size"] > 50) {
      $result["status"] = "200";
      $result["message"]= "Error: Document size exceeds maximum limit of 5 MB.  Please reduce the file size and retry upload";
        }

     //initial code that worked but only does it through php.ini
      /*    elseif ($_FILES['file']['error'] == UPLOAD_ERR_INI_SIZE) {
           $result["status"] = "200";
              $result["message"]= "The file is too big!";/**/
     } else {
             $result["status"] = "500";
           $result["message"]= "Unknown error!";
       }
      }
     ?>
    </body>
    </html>



     <script>
      $(function () {
            $('#successMessage', window.parent.document).html('<?php echo htmlspecialchars($result["message"]); ?>');
     });
    </script>

 function submitForm1(upload_input_field){
  //alert("works");
   upload_input_field.form.submit();
   upload_input_field.disabled = true;
   return true;

 }

1 个答案:

答案 0 :(得分:3)

upload_max_filesize处于PHP_INI_PERDIR模式,并且来自PHP文档:

  

PHP_INI_PERDIR 可以在php.ini,.htaccess,httpd.conf或.user.ini中设置条目(自PHP 5.3开始)

因此,您的选择是php.ini,.htaccess,httpd.conf或.user.ini,您无法使用ini_set在脚本中设置它。