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

프리터코더·2025년 5월 25일
0

php 문제 해결

목록 보기
17/79

PHP.ini 설정

1. 기본 설정 변경

upload_max_filesize = 64M
post_max_size = 64M
memory_limit = 128M
max_execution_time = 300

2. 런타임 설정

ini_set('upload_max_filesize', '64M');
ini_set('post_max_size', '64M');
ini_set('memory_limit', '128M');
ini_set('max_execution_time', '300');

.htaccess 설정

php_value upload_max_filesize 64M
php_value post_max_size 64M
php_value memory_limit 128M
php_value max_execution_time 300

파일 업로드 처리

class FileUploader {
    private $maxSize = 64000000; // 64MB in bytes
    
    public function upload($file) {
        if ($file['size'] > $this->maxSize) {
            throw new Exception('File size exceeds limit');
        }
        
        // 업로드 처리
        $destination = 'uploads/' . basename($file['name']);
        move_uploaded_file($file['tmp_name'], $destination);
        
        return $destination;
    }
}

대용량 파일 처리

function handleLargeFile($file) {
    $chunk = 1024 * 1024; // 1MB 단위로 처리
    
    $handle = fopen($file['tmp_name'], 'rb');
    $fp = fopen('uploads/' . $file['name'], 'wb');
    
    while (!feof($handle)) {
        fwrite($fp, fread($handle, $chunk));
    }
    
    fclose($handle);
    fclose($fp);
}

프로그레스 모니터링

function getUploadProgress($id) {
    $key = ini_get("session.upload_progress.prefix") . $id;
    return $_SESSION[$key] ?? null;
}

실제 구현 예시

// 파일 업로드 폼
<form action="upload.php" method="post" enctype="multipart/form-data">
    <input type="hidden" name="MAX_FILE_SIZE" value="64000000">
    <input type="file" name="userfile">
    <input type="submit" value="Upload">
</form>

// 업로드 처리
if ($_FILES['userfile']['error'] === UPLOAD_ERR_OK) {
    $uploader = new FileUploader();
    try {
        $path = $uploader->upload($_FILES['userfile']);
        echo "File uploaded successfully to: " . $path;
    } catch (Exception $e) {
        echo "Upload failed: " . $e->getMessage();
    }
}
profile
일용직 개발자. freetercoder@gmail.com

0개의 댓글