分块上传不适用于大文件 - Laravel 5.8
Chunk upload does not work with large files - Laravel 5.8
我需要将大文件上传到我的站点,为此,我使用了 Dropzone JS,以及 pion/laravel-chunk-upload,我不明白,一切都是不错,确实如此,但是尽管如此,任何大文件的上传都没有完成,上传小文件时,我得到了一个结果,但是当我尝试使用更大的文件(例如 5MB)时,
- 它在上传的一部分停止 主机例如 (Hostinger)
- 不工作并给出来自laravel验证器的错误WampServer 4(本地主机)
I tried here to remove my Validator, but the same problem, I can't
upload or check if is a valid file or something like that! (for localhost)
我尝试了很多,但我不明白这个问题,找不到解决办法,请帮忙,这是我的代码:
我的看法:
<form action="{{ route('files') }}" enctype="multipart/form-data" class="dropzone" id="fileupload" method="POST">
@csrf
<input type="hidden" name="item_id" value="{{ $item->id }}">
<div class="fallback">
<input name="file" type="files" multiple />
</div>
</form>
控制器:
// UPLOAD FILES
protected function uploadFiles(Request $request) {
$validator = Validator::make($request->all(), [
'file' => 'required|max:3145730', // 3GB
'item_id' => 'required|numeric'
]);
$item_id = $request->item_id;
$item_data = Item::whereId($item_id)->where('user_id', Auth::id())->whereStatus(0)->first();
if (!$item_data || $validator->fails()) {
return response()->json([
'status' => true,
'error' => 'Invalid data!'
], 401);
}
if ($request->hasFile('file')) {
# CHECK IF IS FILE
if ($request->file('file')->isValid()) {
$file = $request->file('file');
# UPLOAD
$type = strtolower($file->getClientOriginalExtension());
$mime = $file->getMimeType();
$size = $file->getSize();
$width = null;
$height = null;
if (!in_array($type, ['png', 'jpeg', 'jpg', 'zip']) || !in_array($mime, ['application/octet-stream', 'application/zip', 'image/jpg', 'image/png', 'image/jpeg'])) {
return response()->json([
'status' => true,
'error' => 'You can\'t upload files of this type.'
], 401);
}
// create the file receiver
$receiver = new FileReceiver("file", $request, HandlerFactory::classFromRequest($request));
// check if the upload is success, throw exception or return response you need
if ($receiver->isUploaded() === false) {
throw new UploadMissingFileException();
}
// receive the file
$save = $receiver->receive();
// check if the upload has finished (in chunk mode it will send smaller files)
if ($save->isFinished()) {
// save the file and return any response you need, current example uses `move` function. If you are
// not using move, you need to manually delete the file by unlink($save->getFile()->getPathname())
if (in_array($type, ['png', 'jpeg', 'jpg'])) {
list($width, $height) = getimagesize($file);
}
return $this->saveFile($save->getFile(), $item_id, $type, $mime, $size, $width, $height);
}
// we are in chunk mode, lets send the current progress
/** @var AbstractHandler $handler */
$handler = $save->handler();
return response()->json([
"done" => $handler->getPercentageDone(),
'status' => true
]);
}
}
return response()->json([
'status' => true,
'error' => 'Invalid data! Please upload a valid file.'
], 401);
}
JS:
Dropzone.prototype.defaultOptions.dictDefaultMessage = "DRAG & DROP FILES HERE TO UPLOAD";
var myDropzone = new Dropzone("#fileupload", {
acceptedFiles: ".jpg, .jpeg, .png, .zip",
chunking: true,
method: "POST",
maxFilesize: 3072, // 3GB
chunkSize: 10000000, // 10MB
maxFiles: 6,
parallelChunkUploads: true,
});
检查您的 PHP.ini 配置以获得最大大小。
upload_max_filesize = 10M
post_max_size = 11M
我需要将大文件上传到我的站点,为此,我使用了 Dropzone JS,以及 pion/laravel-chunk-upload,我不明白,一切都是不错,确实如此,但是尽管如此,任何大文件的上传都没有完成,上传小文件时,我得到了一个结果,但是当我尝试使用更大的文件(例如 5MB)时,
- 它在上传的一部分停止 主机例如 (Hostinger)
- 不工作并给出来自laravel验证器的错误WampServer 4(本地主机)
I tried here to remove my Validator, but the same problem, I can't upload or check if is a valid file or something like that! (for localhost)
我尝试了很多,但我不明白这个问题,找不到解决办法,请帮忙,这是我的代码:
我的看法:
<form action="{{ route('files') }}" enctype="multipart/form-data" class="dropzone" id="fileupload" method="POST">
@csrf
<input type="hidden" name="item_id" value="{{ $item->id }}">
<div class="fallback">
<input name="file" type="files" multiple />
</div>
</form>
控制器:
// UPLOAD FILES
protected function uploadFiles(Request $request) {
$validator = Validator::make($request->all(), [
'file' => 'required|max:3145730', // 3GB
'item_id' => 'required|numeric'
]);
$item_id = $request->item_id;
$item_data = Item::whereId($item_id)->where('user_id', Auth::id())->whereStatus(0)->first();
if (!$item_data || $validator->fails()) {
return response()->json([
'status' => true,
'error' => 'Invalid data!'
], 401);
}
if ($request->hasFile('file')) {
# CHECK IF IS FILE
if ($request->file('file')->isValid()) {
$file = $request->file('file');
# UPLOAD
$type = strtolower($file->getClientOriginalExtension());
$mime = $file->getMimeType();
$size = $file->getSize();
$width = null;
$height = null;
if (!in_array($type, ['png', 'jpeg', 'jpg', 'zip']) || !in_array($mime, ['application/octet-stream', 'application/zip', 'image/jpg', 'image/png', 'image/jpeg'])) {
return response()->json([
'status' => true,
'error' => 'You can\'t upload files of this type.'
], 401);
}
// create the file receiver
$receiver = new FileReceiver("file", $request, HandlerFactory::classFromRequest($request));
// check if the upload is success, throw exception or return response you need
if ($receiver->isUploaded() === false) {
throw new UploadMissingFileException();
}
// receive the file
$save = $receiver->receive();
// check if the upload has finished (in chunk mode it will send smaller files)
if ($save->isFinished()) {
// save the file and return any response you need, current example uses `move` function. If you are
// not using move, you need to manually delete the file by unlink($save->getFile()->getPathname())
if (in_array($type, ['png', 'jpeg', 'jpg'])) {
list($width, $height) = getimagesize($file);
}
return $this->saveFile($save->getFile(), $item_id, $type, $mime, $size, $width, $height);
}
// we are in chunk mode, lets send the current progress
/** @var AbstractHandler $handler */
$handler = $save->handler();
return response()->json([
"done" => $handler->getPercentageDone(),
'status' => true
]);
}
}
return response()->json([
'status' => true,
'error' => 'Invalid data! Please upload a valid file.'
], 401);
}
JS:
Dropzone.prototype.defaultOptions.dictDefaultMessage = "DRAG & DROP FILES HERE TO UPLOAD";
var myDropzone = new Dropzone("#fileupload", {
acceptedFiles: ".jpg, .jpeg, .png, .zip",
chunking: true,
method: "POST",
maxFilesize: 3072, // 3GB
chunkSize: 10000000, // 10MB
maxFiles: 6,
parallelChunkUploads: true,
});
检查您的 PHP.ini 配置以获得最大大小。
upload_max_filesize = 10M
post_max_size = 11M