ahmeda's avatar

How to upload font file in Laravel

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" class="button green">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 the 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 the file to pdf_font.ttf

0 likes
6 replies
Nakov's avatar
Nakov
Best Answer
Level 73

Yes you can, just make a name:

$name = 'pdf_font.' . $request->file->getClientOriginalExtension();

$request->file->storeAs('fonts', $name, 'public');

This should work.

ahmeda's avatar

@Nakov new error fopen(/Users/ald/Documents/Sites/pro/public/fonts/pdf_font.ttf): Failed to open stream: Is a directory

Nakov's avatar

@Jean_ali Remove the directory that you showed before with my first answer that created the issue for you.

https://ibb.co/jRhN8PL

the pdf_font.ttf should be manually removed now.

1 like

Please or to participate in this conversation.