0

I want the uploaded file to be located in the public/uploads folder directly like public/uploads/my_file.jpeg. Why is it that my code uploads it to public/uploads/file_name/file.jpeg?

here is the filesystems.php.

    'public_uploads' => [
        'driver' => 'local',
        'root'   => public_path() . '/uploads',
    ],

and here is the controller.

function upload_template(Request $request)
{


    $filee = $request->file('file_upload');
    $file_ext = $filee->extension();

    $file_name = $model->id . "." . $file_ext;

    Storage::disk('public_uploads')->put($file_name, $filee);

}
beginner
  • 2,024
  • 5
  • 43
  • 76

3 Answers3

4

This happened because you specify the directory to store as filename. The file_name, should be the directory name such as images.

Refer to this line :

Storage::disk('public_uploads')->put($file_name, $filee);

So you could change this to :

Storage::disk('public_uploads')->put('images', $filee);
// output : /images/234234234.jpg

You need to provide the file contents in the second argument not file object, try this :

Storage::disk('public_uploads')->put($file_name, file_get_contents($filee));

To specific the file name you can use move() method instead of storage() :

if($request->hasFile('file_upload'))
{
  $filee = $request->file_upload;
  $name = "my_file"; // name here
  $fileName = $name . $filee->getClientOriginalName();
  $filee->move('public_uploads',$fileName);
}
STA
  • 30,729
  • 8
  • 45
  • 59
0
//this is the best way you create a trait with 2 functions saveImage and 
//deleteImage

 public function saveImage($name,$folder){
$extention=$name->getClientOriginalExtension();
$filename=time().'.'.$extention;
$path=public_path().'/'.$folder;
$name->move($path,$filename);
return $filename;

}

public function deleteImage($name,$folder){
$image_path=public_path().'/'.$folder.'/'.$name;
unlink($image_path);

}

  function upload_template(Request $request){
    $file = $request->file_upload;//$request->your input name
     $img=$this->saveImage($file,'uploads');

     //you can use $img for storing the image in database for example 
      User::create([
        'avatar'=>$img
       ])
 }
//don't forget to invoke your trait
kadiro
  • 956
  • 1
  • 10
  • 16
  • 1
    While this code may resolve the OP's issue, it is best to include an explanation as to how your code addresses the OP's issue. In this way, future visitors can learn from your post, and apply it to their own code. SO is not a coding service, but a resource for knowledge. Also, high quality, complete answers are more likely to be upvoted. These features, along with the requirement that all posts are self-contained, are some of the strengths of SO as a platform, that differentiates it from forums. You can edit to add additional info &/or to supplement your explanations with source documentation. – ysf Jun 26 '20 at 19:24
0

I just found it Laravel 5.3 Storage::put creates a directory with the file name.

Need to provide the file contents in the second argument not file object. Tt should be Storage::disk('public_uploads')->put($file_name, file_get_contents($filee));.

beginner
  • 2,024
  • 5
  • 43
  • 76