긍정곰의 연구소

PHP - 파일업로드 용량제한 설정 본문

긍정곰의개발일지/PHP

PHP - 파일업로드 용량제한 설정

긍정곰 2017. 10. 5. 22:52
웹서버를 운영하다보면 아무래도 파일업로드도 가능한 게시판이나 서비스등을 운영할일이 있을것이다.

그런데 보안상이나 혹은 트래픽적인 부분때문에 서버 클라이언트들의 경우 용량제한을 두거나 하는데 이것을 자유롭게 조절할수 있는 방법을 여기 남겨둘까 한다.

일단 파일 업로드시 처리되는 순서는 아래와 같다.

nginx -> PHP

순서이다.

그리고 기본적인 용량 제한은

nginx의 경우 기본(default)은 1M
PHP7의 경우 기본은 2M(Post 용량 제한 기본은 8M)

로 설정되어 있는데 nginx의 경우 명시하지 않을 경우이고, PHP7의 경우 php.ini에 기본설정이 명시되어있다.

파일업로드 설정을 초과했을시 나타나는 에러메세지

nginx에서는 아래와 같이 나타난다.

413 Request Entity Too Large

위와 같은 에러를 뱉는다면 nginx의 파일업로드 설정 제한에 걸렸다는 뜻이므로 해결하려면 nginx의 파일 업로드 설정을 손봐야 하는데 nginx.conf 파일을 열어 아래 구문을 추가해준다.

http {
...
server {
...
client_max_body_size 35M;
...
}
...
}

https 설정시에 일부 웹프로그램의 경우 https를 거치게끔 되어있는데 그렇다 하더라도 http내 설정에서 설정을 해두는것만으로도 괜찮은 모양이다.(아마 쿠키를 공유하기때문인듯 하다.)

php7에서 아래와 같은 에러를 뿜는 경우가 있는데...

Warning: POST Content-Length of 8978294 bytes exceeds the limit of 8388608 bytes in Unknown on line 0

이때는 파일업로드 제한이 문제가 아니라 post_max_size라는 설정항목 제한에 걸린것이기때문에 php.ini를 수정해주면 해결된다.(위에 Post 용량제한을 따로 언급한것 이때문이다.)

PHP 파일 업로드 사이즈 제한은...

php.ini중

...
;;;;;;;;;;;;;;;;
; File Uploads ;
;;;;;;;;;;;;;;;;

; Whether to allow HTTP file uploads.
; http://php.net/file-uploads
file_uploads = On

; Temporary directory for HTTP uploaded files (will use system default if not
; specified).
; http://php.net/upload-tmp-dir
;upload_tmp_dir =

; Maximum allowed size for uploaded files.
; http://php.net/upload-max-filesize
upload_max_filesize = 2M

; Maximum number of files that can be uploaded via a single request
max_file_uploads = 20
...

위 항목들을 입맛에 맞게 수정해주면 되는데 기본설정은 2MB용량에 최대 20개의 업로드 요청을 처리할수있게 되어있다.

용량을 제한을 수정할려면

upload_max_filesize = 2M

항목을, 업로드 요청 갯수 제한을 조절할려면

max_file_uploads = 20

항목을 수정해주면 된다.

포스트 사이즈 제한은...

마찬가지로 php.ini중

...
; Whether PHP will read the POST data.
; This option is enabled by default.
; Most likely, you won't want to disable this option globally. It causes $_POST
; and $_FILES to always be empty; the only way you will be able to read the
; POST data will be through the php://input stream wrapper. This can be useful
; to proxy requests or to process the POST data in a memory efficient fashion.
; http://php.net/enable-post-data-reading
;enable_post_data_reading = Off

; Maximum size of POST data that PHP will accept.
; Its value may be 0 to disable the limit. It is ignored if POST data reading
; is disabled through enable_post_data_reading.
; http://php.net/post-max-size
post_max_size = 8M
...

에서

post_max_size = 8M

항목을 수정해주면 된다.

예를 들어 8메가에서 30메가로 늘릴려면

post_max_size = 30M

으로 변경해주면 된다.


Comments