Skip to content

Instantly share code, notes, and snippets.

@afiqiqmal
Created November 1, 2017 16:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save afiqiqmal/ea39bcd47779871bb07042328a7b2bdf to your computer and use it in GitHub Desktop.
Save afiqiqmal/ea39bcd47779871bb07042328a7b2bdf to your computer and use it in GitHub Desktop.
This is a sample of renaming file in PHP
<?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;
}
......
}
}
<?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