Home > Net >  How to upload font file in Laravel
How to upload font file in Laravel

Time:01-22

I have files like this font.ttf I need to upload them

HTML code:

<form action="{{ route('admin.settings.pdf.update') }}" method="post" enctype="multipart/form-data">
    @csrf
    <input type="file" name="file" accept=".ttf" required>
    <button type="submit" >upload</button>
</form>

Controller:

public function updatePdfFont(Request $request)
{
    $request->validate([
        'file' => 'required|file',
    ]);

    // dd($request->all());
    // rename($request->file, "pdf_font.ttf");

    $request->file->store('fonts', 'public');
}

// public disk is a custom one that goes files to public folder

The problem that I'm facing now when uploading font it uploaded with no extension like this wmRey4YaOLaldchlFV1l6GQylbZArc4xmyy2tXnL

The second thing I need is how can I rename the file before uploading it? like I want to rename file to pdf_font.ttf

CodePudding user response:

use storAs and set name for upload file

CodePudding user response:

In web applications, one of the most common use-cases for storing files is storing user uploaded files such as photos and documents. Laravel makes it very easy to store uploaded files using the store method on an uploaded file instance. Call the store method with the path at which you wish to store the uploaded file:

public function update(Request $request)
{
    $path = $request->file('avatar')->store('avatars');
    return $path;
}

https://laravel.com/docs/8.x/filesystem

  •  Tags:  
  • Related