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


How to set the max upload size value within php (not php.ini)?

我试图将大小限制为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>

上传文件.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;
 }

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进行设置。