1. Go to this page and download the library: Download bkstar123/laravel-uploader library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
use Bkstar123\LaravelUploader\Contracts\FileUpload;
-----------------
protected $fileupload;
public function __construct(FileUpload $fileupload)
{
$this->fileupload = $fileupload;
}
[
'filename' => 'the original file name',
'path' => 'path to file location relative to the disk storage',
'url' => 'public url to access the file in browser',
'disk' => 'name of storage disk'
]
if ($path = $this->fileupload->handle($request, 'photo', $uploadSettings)) {
// Persist $path data to database
return json_encode([]);
}
return json_encode([
'error' => $this->fileupload->uploadError
]);
public function upload(Request $request, FileUpload $fileupload)
{
$data = $fileupload->handle($request, 'image', ['allowedExtensions' => ['jpg', 'png', 'jpeg']]);
if (!$data) {
return response()->json(['error' => $fileupload->uploadError], 422);
}
// Saving data to database
return response()->json(['success' => "{$data['filename']} has been successfully uploaded", 'data' => $data], 200);
}