Skip to content

Instantly share code, notes, and snippets.

@juice49
Created July 2, 2014 10:14
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save juice49/828ec2c72d21748c2846 to your computer and use it in GitHub Desktop.
Save juice49/828ec2c72d21748c2846 to your computer and use it in GitHub Desktop.
Laravel unique filename
/**
* Creates a unique filename by appending a number
*
* i.e. if image.jpg already exists, returns
* image2.jpg
*/
function uniqueFilename($path, $name, $ext) {
$output = $name;
$basename = basename($name, '.' . $ext);
$i = 2;
while(File::exists($path . '/' . $output)) {
$output = $basename . $i . '.' . $ext;
$i ++;
}
return $output;
}
$file = Input::file('file');
$destination = public_path() . '/uploads/images';
$name = uniqueFilename($destination, $file->getClientOriginalName(), $file->getClientOriginalExtension());
$file->move($destination, $name);
@sourovroy
Copy link

The logic you applied here is grate, i use this in my project.
Thank you

@ecountergg
Copy link

Thanks man! You saved my life!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment