Uploader
A Laravel library to provide file upload utilities. A Javascript library builds a complete file upload widget with upload button, drag-and-drop zone, progress bar and result builder. A controller is available to manage uploaded files. Current version: 4.3.0. Project on GitHub. Project on Packagist. This demosite sources available here.
On this site, you can only upload files up to 1MB and having extensions png, jpg, jpeg, gif, doc, rtf, docx, doc, pdf, txt, htm, html, odt, ogg, mp3, aac, raw, flac, au, zip, gz, xls, ods, csv, ppt, odp, avi, mov, mpg, mpeg, mpa, asf, wma, mp2. Files will be destroyed after 10 minutes.
Upload functions
Package contains utilities that help define file upload functions quickly. It uses Laravel file storages and form validator.
File upload request
Seblhaire\Uploader\FileuploadRequest
is a Laravel form Request
that contain form validation rules that can be used to validate values returned by
our file upload component, namely file size and file type. Use it as follows:
public function index(FileuploadRequest $request){ // process uploaded file
$validated = $request->validated();
...
}
UploaderTrait
Seblhaire\Uploader\UploaderTrait
is a trait that can be used by your
file upload controller.
buildFileObj($filename)
builds an object which contains file name and file extension.buildUniqueFileName($disk, $path, $fileobj, $separator = '-')
verifies if a file already exists in destination path and builds an unique filename.cleanFileName($filename)
removes accentuated characters and white spaces from file name.getPath($request)
returns a string that contains file path defined from default config files or from uploader parameters.getDisk($request, $path)
returns storage name either from default config file or from uploder parameters; if needed, it creates destination directory from path.
Upload controller
Here is a file upload controller that you can use as is or adapt to your needs. In the above code, we return values needed by our complete result processor.
class FileUploadController extends Controller
{
use UploaderTrait; //provides functions to help interact with Uploader objects
public function index(FileuploadRequest $request){ // process uploaded file
$validated = $request->validated();
// define storage and path parameters
$path = $this->getPath($request);
$disk = $this->getDisk($request, $path);
$files = [];
// process files
$errors = [];
foreach ($request->file('file') as $file){
if (!$file->isValid()) { // file upload failed
$errors[] = 'invalid file ' . $file->getClientOriginalName();
continue;
}
// gets object with file info
$fileobj = $this->buildFileObj($this->cleanFileName($file->getClientOriginalName()));
// if a file pattern has been set to replace original filename, build
// another file object
if ($request->has('filepattern') && strlen($request->input('filepattern')) > 0){
$patternobj = $this->buildFileObj($request->input('filepattern'));
}else{
$patternobj = null;
}
if (!is_null($patternobj)){
$destfile = $patternobj;
if ($patternobj->ext == ''){
$destfile->ext = $fileobj->ext;
}
}else{
$destfile = $fileobj;
}
// if file can't be overwitten, build unique file name
if ($request->input('rename')){
$filename = $this->buildUniqueFileName($disk, $path, $destfile);
}else{ // file cam be overwritten
$filename = $destfile->name . '.' . $destfile->ext;
if (Storage::disk($disk)->exists($path . $filename)){
$errors[] = 'file exists ' . $filename;
continue;
}
}
// store file in destination directoty
$filepath = $file->storeAs($path, $filename, $disk);
$files[] = [
'filename' => $filename,
'filepath' => $filepath,
'ext' => $fileobj->ext,
'mimetype' => $file->getMimeType(),
'size' => $file->getSize(),
];
}
if (count($errors)){
$message = implode(', ', $errors);
if (count($files) > 0){
foreach ($files as $file){
Storage::disk($disk)->delete($file['filepath']);
}
}
return response()->json([
'ok' => false,
'message' => $message
]);
}else{
foreach ($files as $i => $file){
// Here you could store file info in a database table.
$files[$i]['file_id'] = random_int(1, 10000);
}
return response()->json([
'ok' => true,
'info' => [
'filepath' => $path,
'disk' => $disk,
'baseurl' => !is_null(config('filesystems.disks.' . $disk . '.url')) ? (config('filesystems.disks.' . $disk . '.url') . $path) : '',
],
'files' => $files,
// other parameters you need can be
]);
}
}
public function processFile(Fileupload2Request $request)
{
$validated = $request->validated();
$path = $this->getPath($request);
$disk = $this->getDisk($request, $path);
$files = [];
$errors = [];
foreach ($request->file('file') as $file){
if (!$file->isValid()) { // file upload failed
$errors[] = 'invalid file ' . $file->getClientOriginalName();
continue;
}
$fileobj = $this->buildFileObj($this->cleanFileName($file->getClientOriginalName()));
if ($request->has('filepattern') && strlen($request->input('filepattern')) > 0){
$patternobj = $this->buildFileObj($request->input('filepattern'));
}else{
$patternobj = null;
}
if (!is_null($patternobj)){
$destfile = $patternobj;
if ($patternobj->ext == ''){
$destfile->ext = $fileobj->ext;
}
}else{
$destfile = $fileobj;
}
if ($request->input('rename')){
$filename = $this->buildUniqueFileName($disk, $path, $destfile);
}else{ // file cam be overwritten
$filename = $destfile->name . '.' . $destfile->ext;
if (Storage::disk($disk)->exists($path . $filename)){
$errors[] = 'file exists ' . $filename;
continue;
}
}
// store file in destination directoty
$filepath = $file->storeAs($path, $filename, $disk);
$files[] = [
'filename' => $filename,
'filepath' => $filepath,
'ext' => $fileobj->ext,
'mimetype' => $file->getMimeType(),
'size' => $file->getSize(),
];
}
if (count($errors)){
$message = implode(', ', $errors);
if (count($files) > 0){
foreach ($files as $file){
Storage::disk($disk)->delete($file['filepath']);
}
}
return response()->json([
'ok' => false,
'message' => $message
]);
}else{
foreach ($files as $i => $file){
// Here you could store file info in a database table.
$files[$i]['file_id'] = random_int(1, 10000);
}
return response()->json([
'ok' => true,
'info' => [
'filepath' => $path,
'disk' => $disk,
'baseurl' => !is_null(config('filesystems.disks.' . $disk . '.url')) ? (config('filesystems.disks.' . $disk . '.url') . $path) : '',
],
'files' => $files,
]);
}
}