<?php
namespace App\Http\Controllers;
use App\Models\ImageProduct;
use Illuminate\Http\Request;
class UploadFileController extends Controller
{
public function inputfile(Request $request){
//pengecekan apakah request file ada atau tidak
if($request->has('file')){
//panggil function upload image akan mengembalikan nama file yang akan di simpan ke db
$fileName = $this->uploadFile($request->file,'data');
//simpan nama file ke db
ImageProduct::create([
"name" => "excel",
"file" => $fileName,
"enable" => 1
]);
}
return response()->json('Berhasil');
}
private function uploadFile($pathFile, $tujuan_upload)
{
$nama_file = $pathFile->getClientOriginalName();
$fileName = $nama_file;
$tujuan_upload = str_replace("uploads/", "", $tujuan_upload);
$tujuan_upload = "file-excel/$tujuan_upload";
$url = $pathFile->storeAs('public/'.$tujuan_upload, $fileName);
if($url){
return $fileName;
}else{
return null;
}
}
}