Created
July 2, 2014 10:14
-
-
Save juice49/828ec2c72d21748c2846 to your computer and use it in GitHub Desktop.
Laravel unique filename
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
/** | |
* 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; | |
} |
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
$file = Input::file('file'); | |
$destination = public_path() . '/uploads/images'; | |
$name = uniqueFilename($destination, $file->getClientOriginalName(), $file->getClientOriginalExtension()); | |
$file->move($destination, $name); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The logic you applied here is grate, i use this in my project.
Thank you