Created
November 1, 2017 16:49
-
-
Save afiqiqmal/ea39bcd47779871bb07042328a7b2bdf to your computer and use it in GitHub Desktop.
This is a sample of renaming file in PHP
This file contains 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 | |
class Sample{ | |
public function test(Request $request) | |
{ | |
..... | |
if ($request->hasFile('logo')) { | |
$file = $request->file('logo'); | |
$filePath = Utils::getFile($file, false, true); | |
Storage::disk('image')->put($filePath, File::get($file)); | |
$company->logo = $filePath; | |
} | |
...... | |
} | |
} |
This file contains 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\Library; | |
use Carbon\Carbon; | |
class Utils | |
{ | |
public static function getFile($file, $add_time = true, $replace_space = false) | |
{ | |
$extension = $file->getClientOriginalExtension(); | |
$filePath = pathinfo($file->getClientOriginalName(), PATHINFO_FILENAME); | |
if ($add_time) { | |
$timestamp = Carbon::now()->format('YmdHsiu'); | |
$filePath .='_'.$timestamp; | |
} | |
$filePath .= '.' . $extension; | |
if ($replace_space) { | |
$filePath = preg_replace('/\s+/', '_', $filePath); | |
} | |
return $filePath; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment