jeudi 23 mai 2019

File storage issue

I have recently started to work with laravel voyager admin package, and I have an issue about file upload mechanism.

Firstly, I have create a folder on media menu "contacts": https://i.imgur.com/fkaVqBz.png

After that, I have made a controller to deal with the contact form and which they can upload their curriculum vitae: https://i.imgur.com/ckCCSOH.png

In my ContactController, I have made some validations regarding the uploaded file, and some modifications on the name of the uploaded file if the user send the post request:

<?php

namespace App\Http\Controllers;

use App\Contact;
use Illuminate\Http\Request;
use App\Rules\Captcha;

class ContactController extends Controller
{
    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function PostContact(Request $request) {
        //
        $this->validate($request, [
            'nom' => 'required|min:2|max:50',
            'prenom' => 'required|min:2|max:50',
            'email' => 'required|email',
            'organisme' => 'required',
            'fonction' => 'required',
            'pays' => 'required',
            'ville' => 'required',
            'telephone' => 'required|numeric|',
            'objet' => 'required',
            'fichier' => 'sometimes|nullable|mimes:doc,docx,pdf',
            'g-recaptcha-response' => new Captcha(),
            'message' => 'required|min:10']);

        $contact = new Contact;

        $contact->nom = $request->input('nom');
        $contact->prenom = $request->input('prenom');
        $contact->organisme = $request->input('organisme');
        $contact->fonction = $request->input('fonction');
        $contact->pays = $request->input('pays');
        $contact->ville = $request->input('ville');     
        $contact->email = $request->input('email');
        $contact->telephone = $request->input('telephone');
        $contact->objet = $request->objet;

        if ($request->hasFile('fichier')) {

            $filenameWithExt = $request->file('fichier')->getClientOriginalName();

            $filename = pathinfo($filenameWithExt, PATHINFO_FILENAME);

            $extension = $request->file('fichier')->getClientOriginalExtension();

            $fileNameToStore = $filename.'_'.time().'.'.$extension;

            $path = $request->file('fichier')->storeAs('contacts', $fileNameToStore);

            $contact->fichier = $path;
        }

        $contact->message = $request->input('message');

        $contact->save();

        return redirect('contact')->with('status', 'Votre message a été envoyé avec succès !!');
    }
}

The CV file is successfully uploaded but not in the right destination, and contacts folder is creation in storage\app: https://i.imgur.com/YPpzfCD.png https://i.imgur.com/KV2CNXo.png

When I create a new contact via voyager admin panel (contact bread) https://i.imgur.com/Q3GBom8.png, the file uploaded goes into public\contacts\month-year-folder (May2019)\ and this is what I hope to do also using my contact form. https://i.imgur.com/bFKSKzq.png

Can someone help me solving this issue?

PS: the command php artisan storage:link indicate that the "public/storage" directory already exists.

Also, I will show any file content if needed

Thanks.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire