Skip to content

Instantly share code, notes, and snippets.

@johnhout
Created June 30, 2015 09:07
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save johnhout/b002acccffb93c8a1d6f to your computer and use it in GitHub Desktop.
Save johnhout/b002acccffb93c8a1d6f to your computer and use it in GitHub Desktop.
Trait for simple file uploading for Laravel.
<?php
use Symfony\Component\HttpFoundation\File\UploadedFile;
trait SimpleUploadTrait {
/**
* Upload the file with slugging to a given path
*
* @param UploadedFile $image
* @param $path
* @return string
*/
public function uploadFile(UploadedFile $image, $path)
{
$extension = $image->getClientOriginalExtension();
$name = pathinfo($image->getClientOriginalName(), PATHINFO_FILENAME);
$image_name = str_slug(date('Y-m-d-h-i-s') . $name . str_random()) . '.' . $extension;
$image->move($path, $image_name);
return $image_name;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment