대용량 파일은 PHP를 사용하여 두 가지 방법으로 업로드할 수 있습니다. 둘 다 아래에서 논의됩니다 -
- php.ini 파일에서 upload_max_filesize 제한을 변경합니다.
- 파일 청크 업로드를 구현하면 업로드가 완료되면 업로드를 더 작은 조각으로 분할하여 이러한 조각을 조립합니다.
php.ini 파일은 아래와 같이 업데이트할 수 있습니다 -
upload_max_filesize = 50M post_max_size = 50M max_input_time = 300 max_execution_time = 300
이는 서버 및 기타 프로젝트의 설정도 변경하므로 피해야 합니다.
htacess 파일 업데이트
php_value upload_max_filesize 50M php_value post_max_size 50M php_value max_input_time 300 php_value max_execution_time 300
인라인 설정 변경 -
<?php
// changing the upload limits
ini_set('upload_max_filesize', '50M');
ini_set('post_max_size', '50M');
ini_set('max_input_time', 300);
ini_set('max_execution_time', 300);
// destination folder is set
$source = $_FILES["file-upload"]["tmp_name"];
$destination = $_FILES["file-upload"]["name"];
// uploaded folder is moved to the destination
move_uploaded_file($source, $destination);
?> 청킹
이 과정에서 큰 파일을 작은 부분으로 분할한 다음 업로드합니다. 'Plupload' 라이브러리를 다운로드하여 사용할 수 있습니다.
<?php
// the response function
function verbose($ok=1,$info=""){
// failure to upload throws 400 error
if ($ok==0) { http_response_code(400); }
die(json_encode(["ok"=>$ok, "info"=>$info]));
}
// invalid upload
if (empty($_FILES) || $_FILES['file']['error']) {
verbose(0, "Failed to move uploaded file.");
}
// upload destination
$filePath = __DIR__ . DIRECTORY_SEPARATOR . "uploads";
if (!file_exists($filePath)) {
if (!mkdir($filePath, 0777, true)) {
verbose(0, "Failed to create $filePath");
}
}
$fileName = isset($_REQUEST["name"]) ? $_REQUEST["name"] : $_FILES["file"]["name"];
$filePath = $filePath . DIRECTORY_SEPARATOR . $fileName;
// dealing with the chunks
$chunk = isset($_REQUEST["chunk"]) ? intval($_REQUEST["chunk"]) : 0;
$chunks = isset($_REQUEST["chunks"]) ? intval($_REQUEST["chunks"]) : 0;
$out = @fopen("{$filePath}.part", $chunk == 0 ? "wb" : "ab");
if ($out) {
$in = @fopen($_FILES['file']['tmp_name'], "rb");
if ($in) {
while ($buff = fread($in, 4096)) { fwrite($out, $buff); }
} else {
verbose(0, "Failed to open input stream");
}
@fclose($in);
@fclose($out);
@unlink($_FILES['file']['tmp_name']);
} else {
verbose(0, "Failed to open output stream");
}
// check if file was uploaded
if (!$chunks || $chunk == $chunks - 1) {
rename("{$filePath}.part", $filePath);
}
verbose(1, "Upload OK");
?> 크기가 500MB를 초과하는 파일을 업로드하려고 하면 성공적으로 업로드됩니다.