Loda da Sarrafa Fayil da Hoto a ciki Laravel

Ƙayyade filin lodawa a cikin tsari

Da fari dai, ƙara <input type="file"> filin zuwa fom ɗin HTML don baiwa masu amfani damar zaɓar fayil ko hoto don lodawa.

<form method="POST" action="{{ route('upload') }}" enctype="multipart/form-data">  
    @csrf  
    <input type="file" name="file">  
    <button type="submit">Upload</button>  
</form>  

 

Karɓar buƙatar lodawa

A cikin Laravel mai sarrafawa, zaku iya sarrafa buƙatar lodawa ta hanya. Yi amfani da Illuminate\Http\Request abin don samun isa ga fayil ɗin da aka ɗora da aiwatar da ayyukan kulawa da suka dace.

use Illuminate\Http\Request;  
  
public function upload(Request $request)  
{  
    if($request->hasFile('file')) {  
        $file = $request->file('file');  
        // Handle the file here  
    }  
}  

 

Ajiye fayil ɗin

Laravel yana ba da store hanyar adana fayil ɗin da aka ɗora. Kawai kira wannan hanyar akan abun fayil kuma samar da hanyar ajiya da ake so.

$path = $file->store('uploads');

 

Riƙe hoton

Idan kana buƙatar sarrafa hoto, kamar sake girman girma, yankewa, ko amfani da tacewa, zaku iya amfani da ɗakin karatu na sarrafa hoto kamar Hoton Intervention. Da farko, shigar da fakitin Hoto Intervention ta Mawaƙi:

composer require intervention/image

Sannan, zaku iya amfani da hanyoyin ɗakin karatu don aiwatar da hoton.

use Intervention\Image\Facades\Image;  
  
public function upload(Request $request)  
{  
    if($request->hasFile('file')) {  
        $file = $request->file('file');  
        $image = Image::make($file);  
        // Handle the image here  
    }  
}  

 

Nuna fayil ɗin da aka ɗora da hoton

A ƙarshe, zaku iya nuna fayil ɗin da aka ɗora da hoton a cikin mahallin mai amfani. Yi amfani Laravel da hanyoyin taimakon don samar da URL na jama'a don fayil da hoton da aka adana, da amfani da su a cikin HTML ko CSS.

$url = asset('storage/'. $path);

 

Kuna iya amfani da $url m a cikin HTML ko CSS don nuna fayil ko hoton da aka ɗora.

 

Ta bin waɗannan matakan da amfani da Laravel abubuwan ginannun abubuwan ciki, zaku iya lodawa da sarrafa fayiloli da hotuna cikin sauƙi cikin Laravel aikace-aikacenku.