22.11.2018 11:15

Laravel ile dosya yükleme (Upload)


HTML KISMI

 
<form action="" method="post"  enctype="multipart/form-data">
	@csrf
	<div class="form-group">
		<label for="img">Fotoğraf</label>
		<input type="file" name="img" value="#" class="form-control">
	</div>

	<div class="form-group">
	    <button class="btn btn-success">Yükle</button>
	</div>

</form>

CONTROLLER KISMI

public function upload(Request $req){        
        $image = $req->file('img');
        $name = $slug.'--'.rand(0,9999).'.'.$image->getClientOriginalExtension();
        $destinationPath = public_path('/uploads/img/fotolar');
        $imagePath = $destinationPath. "/".  $name;
        $image->move($destinationPath, $name);

        return $name;
}

ÇOKLU EKLEME YAPMAK İÇİN

<input type="file" name="img[]" value="#" class="form-control" multiple>
public function upload(Request $req){   
   foreach($req->file('img') as $img){  
        $image = $img;
        $name = $slug.'--'.rand(0,9999).'.'.$image->getClientOriginalExtension();
        $destinationPath = public_path('/uploads/img/fotolar');
        $imagePath = $destinationPath. "/".  $name;
        $image->move($destinationPath, $name);
   }
}