Last active
October 17, 2023 13:21
-
-
Save hmawla/5c1b442ea212c138b81eab1823e0971e to your computer and use it in GitHub Desktop.
Laravel Files Controller
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App; | |
use Illuminate\Http\UploadedFile; | |
use Illuminate\Support\Facades\Storage; | |
use Ramsey\Uuid\Uuid; | |
class FilesController | |
{ | |
public static function uploadFile(UploadedFile $file, $directory, $extension = null){ | |
$filePath = $file->storeAs('public/' . $directory, Uuid::uuid1() . '.' . ($extension ?? explode('/', $file->getMimeType())[1])); | |
return Storage::url($filePath); | |
} | |
public static function uploadFileWithName(UploadedFile $file, $directory, $extension = null){ | |
$name = Uuid::uuid1() . '.' . ($extension ?? explode('/', $file->getMimeType())[1]); | |
$filePath = $file->storeAs('public/' . $directory, $name); | |
return ['url' => Storage::url($filePath), 'name' => $name]; | |
} | |
public static function deleteFile($fileName, $directory){ | |
$imagePath = 'public/' . $directory . '/' . $fileName; | |
if(Storage::exists($imagePath)) | |
Storage::delete($imagePath); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment