Renamify
Laravel package for renaming file before uploaded on server.
Renamify
is a package for Laravel used to rename a file before uploaded to prevent replacing exists file which has the same name to this new uploaded file. eg
: If in a given file destination has a file called photo.jpg
, new file name will be photo_1.jpg
.
Getting Started
Requirements
- PHP >= 7.2.5
- Composer is required
- Laravel 7.x, 8.x and 9.x
Installation
This package can be installed through composer require
. Before install this, make sure that your are working with PHP >= 7.1 in your system. Just run the following command in your cmd or terminal:
composer require mbere250/laravel-renamify
After you have installed Renamify
package, open your Laravel config file config/app.php and add the following line.
In the $providers array add the service providers for this package.
Mbere250\Renamify\Renamify::class,
Package initialization
Import Renamify
package on controller using below line:
use Mbere250\Renamify\Renamify;
Usage
- Create routes
routes/web.php
Route::view('/upload_view','upload_view')->name('upload_view');
Route::post('/upload_handler',[UploadController::class,'upload_handler'])->name('upload_handler');
- Create view
resources/views/upload_view.blade.php
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Upload file</title>
<link rel="stylesheet" href="/bootstrap-3.1.1/css/bootstrap.min.css">
</head>
<body>
<div class="container">
<div class="row mt-3">
<div class="col-md-6 col-md-offset-3 mt-3">
<h4 class="text-center">Upload file</h4>
<hr>
<form action="{{ route('upload_handler') }}" method="POST" enctype="multipart/form-data" id="uploadForm">
@csrf
<div class="form-group">
<label for="my-input">file</label>
<input id="my-input" class="form-control" type="file" name="file">
<span class="text-danger error-text imagefile_error"></span>
</div>
<div class="form-group">
<button type="submit" class="btn btn-primary">Upload</button>
</div>
</form>
</div>
</div>
</div>
<script src="/jquery-3.0.0.min.js"></script>
<script src="/bootstrap-3.1.1/js/bootstrap.min.js"></script>
</body>
</html>
app/Http/Controllers/UploadController.php
- Controller When you need your file to be uploaded in public path of your application
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\File;
use Exception;
use Mbere250\Renamify\Renamify;
class UploadController extends Controller
{
public function upload_handler(Request $request){
if( $request->hasFile('file') ){
$path = 'uploads/';
if ( !File::exists( public_path($path) ) ) {
File::makeDirectory(public_path($path),0777,true);
}
$file = $request->file('file');
$filename = $file->getClientOriginalName();
//Generate new file name
$new_filename = Renamify::file_path( public_path($path), $filename )->generate();
$upload = $file->move($path, $new_filename );
if($upload){
SavedFile::insert([
'filename' => $new_filename,
]);
}
}else{
throw new Exception(' No file selected. ');
}
}
}
When you need your file to be uploaded in public storage path of your application:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\File;
use Exception;
use Mbere250\Renamify\Renamify;
class UploadController extends Controller
{
public function upload_handler(Request $request){
if( $request->hasFile('file') ){
$path = 'uploads/';
if ( !File::exists(storage_path('public/'.$path)) ) {
File::makeDirectory(storage_path('public/'.$path),0777,true);
}
$file = $request->file('file');
$filename = $file->getClientOriginalName();
//Generate new file name
$new_filename = Renamify::file_path( storage_path('app/public/').$path , $filename )->generate();
$upload = $file->storeAs( 'public/'.$path,$new_filename);
if($upload){
SavedFile::insert([
'filename' => $new_filename,
]);
}
}else{
throw new Exception(' No file selected. ');
}
}
}
When you are using this package, you will be able to get file name as shown in the image below:
License
Renamify
is released under the MIT License.