【问题标题】:How to set the max upload size value within php (not php.ini)? [duplicate]如何在 php(不是 php.ini)中设置最大上传大小值? [复制]
【发布时间】:2016-08-04 16:08:58
【问题描述】:

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

 }

【问题讨论】:

  • 快速回答:.htaccess / .user.ini ... 脚本内部不起作用。
  • 尝试if($_FILES["file"]["size"] &gt; 50) 然后使用else/elseif,您现在正在做的是首先允许更大的文件,而不是检查它是否首先超过 50。
  • 您可以使用 ini_set 而不是实际编辑 php.ini 文件。
  • 我以前看过它......所以这不是基于其他帖子的重复。我正在使用 php 5.3。
  • 这不是重复的,我找到了答案。这必须在表单内和 之上

标签: php


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-06-07
    • 2013-10-09
    • 1970-01-01
    • 2014-08-14
    • 1970-01-01
    • 2017-01-22
    • 1970-01-01
    • 2017-03-12
    相关资源
    最近更新 更多