t0berius's avatar

file upload not working

I'm working on a support ticket system. User should be able to upload a file to each ticket reply.

My view:

@if($errors->has()) @foreach ($errors->all() as $error) {{ $error }} @endforeach @endif

@postTicket') }}" enctype="multipart/form-data">
<div>
    Titel:
    <input type="text" maxlength="50" name="title" value="{{ old('title') }}">
</div>

<div>
    Frage:
    <textarea name="question">
        {{ old('question') }}
    </textarea>
</div>

<div>
    Abteilung:

    <select name="departments">
        @foreach ($departments as $department)
            <option value="{{ $department->id }}">{{ $department->name }}</option>
        @endforeach
        <option value="3">MÜLL</option>
        
    </select>
</div>

<div>
    Attachment:
    <input type="file" name="attachment">
</div>

<div>
    <button type="submit">Absenden</button>
</div>

my controller:

 public function postTicket(Request $request)
{
    $validator = Validator::make($request->all(), [
    'departments' => 'required|exists:departments,id',
    'attachment' =>'max:1000',
    ]);

    if ($validator->fails()) 
       return back()->withErrors($validator)->withInput();
    

    if ($request->hasFile('attachment')) 
    {
        if ($request->file('attachment')->isValid()) 
        {

            $file = Request::file('attachment');
            $extension = $file->getClientOriginalExtension();
            Storage::disk('local')->put($file->getFilename().'.'.$extension,  File::get($file));
            $entry = new Fileentry();
            $entry->mime = $file->getClientMimeType();
            $entry->filename = $file->getFilename().'.'.$extension;
            $entry->save();

            dd("uploaded");

            
        }

        dd("upload failed");
    }

    dd("no file");
}

New files should be saved "protected" into the storage system. All the time I check my code it says "no file". Any idea I make a mistake?

0 likes
7 replies
kingpabel's avatar

Please clear what is the problem. Can't you upload the project or can't you find the project?

t0berius's avatar

The upload just don't work, as I told it returns all the time "no file" after I selected a file to upload.

ehsanquddusi's avatar

Your form should have enctype attribute set to support file uploads.

<form action="/upload" method="post" enctype="multipart/form-data">
1 like
t0berius's avatar

I've edited code above. Now my validator on departments fails ALLthe time I upload a file. Before I added

enctype="multipart/form-data"

it works fine.

Please or to participate in this conversation.