Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't upload images to a public directory in laravel

I'm trying to upload images into a Laravel app that will then need to be publicly displayed. However, I seem to only be able to upload to a folder within the application root. Any attempt to upload to the public directory throws the following error:

protected function getTargetFile($directory, $name = null)
{
    if (!is_dir($directory)) {
        if (false === @mkdir($directory, 0777, true)) {
            throw new FileException(sprintf('Unable to create the "%s" directory', $directory));

I'm assuming this means that Laravel is going to prevent me from uploading to any file that is publicly accessible. I actually rather like this, however, how can I link to images that are outside of the public directory I've tried to ../ my way out of the public folder, but appropriately, that didn't work.

Alternately, anything that will let me uploaded directly to the public folder would be great too.

If it helps, here is my controller method that puts the file in public folder:

$thumbnail_file = Input::file('thumbnail'); 
$destinationPath = '/uploads/'. str_random(8);
$thumbnail_filename = $thumbnail_file->getClientOriginalName();
$uploadSuccess = Input::file('thumbnail_url')->move($destinationPath, $thumbnail_filename);
like image 537
cfkane Avatar asked Feb 18 '14 12:02

cfkane


1 Answers

Route::post('upload', function(){
   $avarta = Input::file('avatar');
   if(strpos($avarta->getClientMimeType(),'image') !== FALSE){

      $upload_folder = '/assets/uploads/';

      $file_name = str_random(). '.' . $avarta->getClientOriginalExtension();

      $avarta->move(public_path() . $upload_folder, $file_name);

      echo URL::asset($upload_folder . $file_name);  // get upload file url

      return Response::make('Success', 200);
   }else{
     return Response::make('Avatar must be image', 400); // bad request
  }});

will upload to /public/assets/uploads folder, maybe help you

like image 145
vuhung3990 Avatar answered Sep 28 '22 16:09

vuhung3990