Skip to content

Instantly share code, notes, and snippets.

@hmawla
Last active October 17, 2023 13:21
Show Gist options
  • Save hmawla/5c1b442ea212c138b81eab1823e0971e to your computer and use it in GitHub Desktop.
Save hmawla/5c1b442ea212c138b81eab1823e0971e to your computer and use it in GitHub Desktop.
Laravel Files Controller
<?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