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()) return $this->saveFile($save->getFile()); } // we are in chunk mode, lets send the current progress /** @var AbstractHandler $handler */ $handler = $save->handler(); return response()->json([ "done" => $handler->getPercentageDone(), 'status' => true ]); } /** * Saves the file * * @param UploadedFile $file * * @return JsonResponse */ protected function saveFile(UploadedFile $file) { $fileName = $this->createFilename($file); // Group files by mime type $mime = str_replace('/', '-', $file->getMimeType()); // Group files by the date (week $dateFolder = date("Y-m-W"); // Build the file path $filePath = "upload/{$mime}/{$dateFolder}/"; $disk = Storage::disk('local'); $disk->putFileAs($filePath, $file, $fileName); unlink($file->getPathname()); return response()->json([ 'path' => $filePath, 'name' => $fileName, 'mime_type' => $mime ]); } /** * Create unique filename for uploaded file * @param UploadedFile $file * @return string */ protected function createFilename(UploadedFile $file) { $extension = $file->getClientOriginalExtension(); $filename = str_replace(".".$extension, "", $file->getClientOriginalName()); // Filename without extension // Add timestamp hash to name of the file $filename .= "_" . md5(time()) . "." . $extension; return $filename; } }