Laravel 5: uploading an image to send by email

I have never submitted images that were previously uploaded to a form, so I am in trouble.

I know this line of code: $request->file('image')->move('/', $fileName);

won't do anything because of where I move it, but that as far as I could read these Laravels . Basically, it spits out this error:

Unable to write in the "/" directory

If I were to remove the above code and submit my form, everything works correctly, but the image fields are empty. This leads me to my question:

Where do I move the image and how do I get it from that location so that it can be used in the sent email and not show an empty space as if nothing was uploaded?

Here are my files ...

index.blade.php (only part of image only form):

<div class="form-group">
     {!!  Form::file('image') !!}
</div>

      

emails / contact.blade.php (only part of the image):

<p>
    Image: {{ $image }}
</p>

      

Requests / ContactFormRequest.php:

<?php

namespace App\Http\Requests;

use App\Http\Requests\Request;

class ContactFormRequest extends Request
{
    public function authorize()
    {
        return true;
    }

    public function rules()
    {
        return [
            'name'   => 'required',
            'height' => 'required',
            'age'    => 'required',
            'weight' => 'required',
            'gender' => 'required',
            'email'  => 'required|email',
            'image'  => 'required|mimes:jpeg,jpg,png|max:200px'
        ];
    }
}

      

Auth / AboutController.php:

<?php namespace App\Http\Controllers;

use App\Http\Requests\ContactFormRequest;

class AboutController extends Controller {

    public function create()
    {
        return view('pages.index');
    }

    public function store(ContactFormRequest $request)
    {
        $fileName = "Image";
        $request->file('image')->move('/', $fileName);

        \Mail::send('emails.contact',
            array(
                'name'   => $request->get('name'),
                'height' => $request->get('height'),
                'age'    => $request->get('age'),
                'weight' => $request->get('weight'),
                'gender' => $request->get('gender'),
                'email'  => $request->get('email'),
                'image'  => $request->get('image')
            ), function($message)
        {
            $message->from('example@example.com');
            $message->to('example@example.com', 'Admin')->subject('Example Text');
        });

        return \Redirect::route('/')
            ->with('message', 'Your application is on its way!');
    }

}

      

+3


source to share


1 answer


if you want to attach a picture to post use this:

Mail::send('emails.contact',
        array(
            'name'   => $request->get('name'),
            'height' => $request->get('height'),
            'age'    => $request->get('age'),
            'weight' => $request->get('weight'),
            'gender' => $request->get('gender'),
            'email'  => $request->get('email'),
            'image'  => $filename
        ), function($message) use($filename)
    {
        $message->from('example@example.com');
        $message->to('example@example.com', 'Admin')->subject('Example Text');
       $message->attach('/'.$filename);
    });

      

I used to do this to load an image.



//Request File
$file = $request->file('image');

//Destination
 $destination = public_path() . '/img/';

//Define the name
$name= "image";

//Get file extension 
$extension = $file->getClientOriginalExtension();

//join the name you set with the extension
$filename = $name . '.' . $extension;

//after that move the file to the directory
$file->move($destination, $filename);

      

If you want to show an image, remember to use (instead of returning its name)

<img src="foo/bar" />

      

+1


source







All Articles