programing

PHP의 'post_max_size'를 초과하는 파일을 정상적으로 처리하는 방법은 무엇입니까?

goodjava 2022. 10. 31. 21:15

PHP의 'post_max_size'를 초과하는 파일을 정상적으로 처리하는 방법은 무엇입니까?

이메일에 파일을 첨부하는 PHP 폼을 작성하고 있으며 업로드한 파일이 너무 큰 경우에는 적절하게 처리하려고 합니다.

두 되었습니다.php.ini업로드의 을 주는 것은 다음과 같습니다.upload_max_filesize ★★★★★★★★★★★★★★★★★」post_max_size

가 exceeds음음음음음음음음음음음음음음음 if if를 초과하는 경우upload_max_filesize 는 를 0.PHP 는 0 으로 되돌립니다.괜찮습니다. 확인해 보겠습니다.

, 하하그그 but를 넘으면post_max_size스크립트는 자동으로 실패하고 공백 형식으로 돌아갑니다.

이 오류를 발견할 수 있는 방법이 있나요?

매뉴얼에서 다음 순서를 수행합니다.

포스트 데이터의 크기가 post_max_size보다 클 경우 $_POST$_FILES 슈퍼글로벌은 비어 있습니다.이것은 다양한 방법으로 추적할 수 있습니다.예를 들어 $_GET 변수를 <form action="edit.dit?dit=1">과 같이 데이터를 처리하는 스크립트에 전달하고 $_GET['processed']가 설정되어 있는지 확인합니다.

PHP가 에러를 송신하는 것은 아닌 것 같습니다.게다가 빈 $_POST 어레이를 송신하고 있기 때문에, 스크립트는 공백의 형태로 돌아옵니다.-POST라고는 생각되지 않습니다.(IMHO의 설계상의 판단은 부적절합니다)

이 해설자는 또한 흥미로운 아이디어를 가지고 있다.

보다 우아한 방법은 post_max_size와 $_SERVER['CONTENT_LENGH']를 비교하는 것입니다.후자는 업로드된 파일 크기 및 포스트 데이터뿐만 아니라 멀티파트 시퀀스도 포함되므로 주의하시기 바랍니다.

최대 포스트 사이즈를 넘는 파일을 캐치/처리하는 방법이 있습니다.이것은 최종 사용자에게 무엇이 일어났는지, 누구에게 잘못이 있는지를 알려주기 때문에 제가 가장 좋아하는 방법입니다.

if (empty($_FILES) && empty($_POST) &&
        isset($_SERVER['REQUEST_METHOD']) &&
        strtolower($_SERVER['REQUEST_METHOD']) == 'post') {
    //catch file overload error...
    $postMax = ini_get('post_max_size'); //grab the size limits...
    echo "<p style=\"color: #F00;\">\nPlease note files larger than {$postMax} will result in this error!<br>Please be advised this is not a limitation in the CMS, This is a limitation of the hosting server.<br>For various reasons they limit the max size of uploaded files, if you have access to the php ini file you can fix this by changing the post_max_size setting.<br> If you can't then please ask your host to increase the size limits, or use the FTP uploaded form</p>"; // echo out error and solutions...
    addForm(); //bounce back to the just filled out form.
}
else {
    // continue on with processing of the page...
}

$_POST 및 $_FILES의 빈 상태를 체크할 수 없는 SOAP 요청도 유효한 요청에서 비어 있기 때문에 문제가 발생했습니다.

따라서 CONTENT_LENGH와 post_max_size를 비교하여 체크를 구현했습니다.느려진 예외는 나중에 등록된 예외 핸들러에 의해 XML-SOAP-FAULT로 변환됩니다.

private function checkPostSizeExceeded() {
    $maxPostSize = $this->iniGetBytes('post_max_size');

    if ($_SERVER['CONTENT_LENGTH'] > $maxPostSize) {
        throw new Exception(
            sprintf('Max post size exceeded! Got %s bytes, but limit is %s bytes.',
                $_SERVER['CONTENT_LENGTH'],
                $maxPostSize
            )
        );
    }
}

private function iniGetBytes($val)
{
    $val = trim(ini_get($val));
    if ($val != '') {
        $last = strtolower(
            $val{strlen($val) - 1}
        );
    } else {
        $last = '';
    }
    switch ($last) {
        // The 'G' modifier is available since PHP 5.1.0
        case 'g':
            $val *= 1024;
            // fall through
        case 'm':
            $val *= 1024;
            // fall through
        case 'k':
            $val *= 1024;
            // fall through
    }

    return $val;
}

@Matt McCormick 및 @AbdullahAJM의 답변을 바탕으로 테스트에 사용되는 변수가 설정되어 있는지 확인하고 $_SERVER['CONTENT_LENGH']가 php_max_filesize 설정을 초과하는지 여부를 확인하는 PHP 테스트 사례를 소개합니다.

            if (
                isset( $_SERVER['REQUEST_METHOD'] )      &&
                ($_SERVER['REQUEST_METHOD'] === 'POST' ) &&
                isset( $_SERVER['CONTENT_LENGTH'] )      &&
                ( empty( $_POST ) )
            ) {
                $max_post_size = ini_get('post_max_size');
                $content_length = $_SERVER['CONTENT_LENGTH'] / 1024 / 1024;
                if ($content_length > $max_post_size ) {
                    print "<div class='updated fade'>" .
                        sprintf(
                            __('It appears you tried to upload %d MiB of data but the PHP post_max_size is %d MiB.', 'csa-slplus'),
                            $content_length,
                            $max_post_size
                        ) .
                        '<br/>' .
                        __( 'Try increasing the post_max_size setting in your php.ini file.' , 'csa-slplus' ) .
                        '</div>';
                }
            }

이것은 이 문제를 해결하는 간단한 방법입니다.

"Check Post Size"로 전화하시면 됩니다.코드 시작 시 Exceeded" (초과)

function checkPostSizeExceeded() {
        if (isset($_SERVER['REQUEST_METHOD']) and $_SERVER['REQUEST_METHOD'] == 'POST' and
            isset($_SERVER['CONTENT_LENGTH']) and empty($_POST)//if is a post request and $_POST variable is empty(a symptom of "post max size error")
        ) {
            $max = get_ini_bytes('post_max_size');//get the limit of post size 
            $send = $_SERVER['CONTENT_LENGTH'];//get the sent post size

            if($max < $_SERVER['CONTENT_LENGTH'])//compare
                throw new Exception(
                    'Max size exceeded! Were sent ' . 
                        number_format($send/(1024*1024), 2) . 'MB, but ' . number_format($max/(1024*1024), 2) . 'MB is the application limit.'
                    );
        }
    }

다음 보조 기능을 복사하십시오.

function get_ini_bytes($attr){
    $attr_value = trim(ini_get($attr));

    if ($attr_value != '') {
        $type_byte = strtolower(
            $attr_value{strlen($attr_value) - 1}
        );
    } else
        return $attr_value;

    switch ($type_byte) {
        case 'g': $attr_value *= 1024*1024*1024; break;
        case 'm': $attr_value *= 1024*1024; break;
        case 'k': $attr_value *= 1024; break;
    }

    return $attr_value;
}

저도 같은 문제가 있어서 이 페이지에 이미 게재되어 있는 몇 가지 솔루션을 조합했습니다(@Doblas, @Lance Cleveland, @AbdullahAJM).

또한 PHP.ini가 경고를 표시하도록 설정되어 있지 않은 경우에만 가능한 200 OK 대신 413 Payload Too Large 오류를 보내려고 합니다.

// Check for Warning: php catch Warning: Unknown: POST Content-Length of bytes exceeds the limit of bytes in Unknown on line 0
// Sending 413 only works, if Warnings are turned off in php.ini!!!

// grab the size limits...
$postMaxSize = trim(ini_get('post_max_size')); 
if (strlen($postMaxSize)>0) {
   $postMaxSizeValue = substr($postMaxSize, 0, -1);
   $postMaxSizeUnit = strtolower(substr($postMaxSize, -1));
   $postMaxSize = 0; // make it fail save
   if (false !== filter_var($postMaxSizeValue, FILTER_VALIDATE_INT, array('options' => array( 'min_range' => 0)))) {
      switch ($postMaxSizeUnit) {
         case 'g': $postMaxSizeValue*=1024; // ... and fall through
         case 'm': $postMaxSizeValue*=1024; // ... and fall through
         case 'k': $postMaxSizeValue*=1024; break;
         default: if ($postMaxSizeUnit>='0' && $postMaxSizeUnit<='9') {
                     $postMaxSizeValue = (int) $postMaxSizeValue.$postMaxSizeUnit;
                  } else {
                     $postMaxSizeValue = 0;
                  }
      }
      $postMaxSize = $postMaxSizeValue;
   }
} else {
   $postMaxSize = 0;
}

if (empty($_FILES) && empty($_POST) &&
    isset($_SERVER['REQUEST_METHOD']) && $_SERVER['REQUEST_METHOD'] == 'POST' &&
    isset($_SERVER['CONTENT_LENGTH']) && $_SERVER['CONTENT_LENGTH'] > $postMaxSize) {
    // if is a post request and $_POST variable is empty(a symptom of "post max size error")
    
    if (headers_sent()) {
       // echo out error and solutions...
       echo "<p style=\"color: #F00;\">\nPlease note that an error <b>413 Payload Too Large</b> should be sent, but the warning can't be catched, and so the client gets a <b>200 OK</b>. ".
            "Please turn off warnings in php.ini in order to achieve the correct behaviour.</p>"; 
    } else {
       http_response_code(413);
    }

    // catch file overload error: echo out error and solutions...
    echo "<p style=\"color: #F00;\">\nPlease note files larger than ".$postMaxSize." will result in this error!<br>".
         "Please be advised this is not a limitation in the script, this is a limitation of the hosting server.</p>";
    exit(1);
}

언급URL : https://stackoverflow.com/questions/2133652/how-to-gracefully-handle-files-that-exceed-phps-post-max-size