<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class FileUpload extends Controller
{
    public function upload(Request $request)
    {
        if ($request->hasFile('upload')) {
            $file = $request->file('upload');
            $originName = $file->getClientOriginalName();
            $extension = $file->getClientOriginalExtension();
            $fileName = pathinfo($originName, PATHINFO_FILENAME) . '_' . time() . '.' . $extension;
            
            $file->move(public_path('uploads'), $fileName);

            $url = asset('uploads/' . $fileName);


            return response()->json([
                'fileName' => $fileName,
                'uploaded' => true,
                'url' => $url
            ]);
        }
    }
}
