MrRobot21's avatar

How to Upload PDF, Doc file using form and send the file to a mail

Hi Awesome People

I wan to upload PDF or Doc file through form and send the same file to a mail or for now i just want to upload the file to database, i really don't know how to achieve it,

I Created a file called "view/careers/apply.blade.php" there is a form with 3 input fields, i can post the name and email to database to with the help of @tykus_ikus (he really helped me a lot) but i don't know how to upload files

<form method="POST" action="apply" class="form-horizontal custm-form" role="form">
{!! csrf_field() !!}
    <div class="modal-header">
    <!--<button type="button" class="close" data-dismiss="modal" aria-label="Close">
            <span aria-hidden="true">×</span>
        </button>-->
        <h4 class="modal-title">Apply Online</h4>
    </div>
    <div class="modal-body">
        <div class="form-group">
            <label class="control-label col-md-3">Name:</label>
            <div class="col-md-8">
                <input type="text" class="form-control" id="name" placeholder="Enter Your Name" name="name">
            </div>
        </div>
        <div class="form-group">
            <label class="control-label col-md-3">E-mail:</label>
            <div class="col-md-8">
                <input type="email" class="form-control" id="email" placeholder="Enter email" name="email">
            </div>
        </div>  
        <div class="form-group">
            <label class="col-md-3 control-label" name="resume">Attach Resume:</label>
            <div class="col-md-8">
                <input  type="file" id="resume" placeholder="Resume" name="resume" class=""/>
                <span class="required" id='spnFileError'></span>
            </div>
        </div>                                
    </div>
    <div class="modal-footer">
        <div class="col-xs-5">
            <p style="margin:0;text-align:left;color: green;display:none;" id="successMsg">Submitted Successfully!</p>
        </div>
        <button type="submit" id="btnUpload" class="custm-btn btn-primary" onclick="uploadFile();">Submit</button>
        <button type="button" class="custm-btn btn-default" data-dismiss="modal">Close</button>
    </div>
</form>

My Model IS "AvoCareer.php"

<?php 

namespace App;

use Illuminate\Database\Eloquent\Model;

class AvoCareer extends Model {
    protected $table = "avo_career";
}

?>

My Controller is "AvoCareersController.php"

<?php 

namespace App\Http\Controllers;

use View;
use App\AvoCareer;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Redirect;
use Illuminate\Routing\Controller as BaseController;

class AvoCareersController extends BaseController {

    public function careerpage() {

        return view('careers.apply');
    }

    public function store(Request $request)
    {

        $avoCareer = new AvoCareer;
        $avoCareer->name = $request->name;
        $avoCareer->email = $request->email;
        $avoCareer->resume = $request->resume;
        $avoCareer->save();

        return redirect('apply');
    
    }

}


?>

And The rought is

Route::get('apply', 'AvoCareersController@careerpage');
Route::post('apply', 'AvoCareersController@store');

Looking forward for Much needed help

Thanks

0 likes
10 replies
MrRobot21's avatar

@bobbybouwmann Thank you very much for your response

I have gone through the article you mentioned it's done using laravel form i don't no much about that

anyway i tried this in my controller "AvoCareersController.php"

<?php 

namespace App\Http\Controllers;

use View;
use App\AvoCareer;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Redirect;
use Illuminate\Routing\Controller as BaseController;

class AvoCareersController extends BaseController {

    public function careerpage() {

        return view('careers.apply');
    }

    public function store(Request $request)
    {

        $avoCareer = new AvoCareer;
        $avoCareer->name = $request->name;
        $avoCareer->email = $request->email;
        $avoCareer->resume = $request->resume;
        $avoCareer->save();

        $fileName = $avoCareer->id . '.' . 
        $request->file('pdf')->getClientOriginalExtension();

        $request->file('pdf')->move(
            base_path() . '/public/uploads', $fileName
        );

        return redirect('apply');
    
    }

}


?>

I am getting the following error,

FatalErrorException in AvoCareersController.php line 28:
Call to a member function getClientOriginalExtension() on a non-object

i am sorry if my questions are silly i am very very new to laravel and backend development i am a core frontend developer so..

Thank you

Frankiec91's avatar

Your upload file is 'resume' instead of 'pdf'

$request->file('pdf') to $request->file('resume')
1 like
MrRobot21's avatar

Hi @Frankiec91 Thanks, but

    public function store(Request $request)
    {

        $avoCareer = new AvoCareer;
        $avoCareer->name = $request->name;
        $avoCareer->email = $request->email;
        $avoCareer->resume = $request->resume;
        $avoCareer->save();

        $fileName = $avoCareer->id . '.' . 
        $request->file('resume')->getClientOriginalExtension();

        $request->file('resume')->move(
            base_path() . '/public/uploads', $fileName
        );

        return redirect('apply');
    
    }

I am still getting this error

FatalErrorException in AvoCareersController.php line 28:
Call to a member function getClientOriginalExtension() on a non-object
Frankiec91's avatar
Level 1

@MrRobot21 please add enctype="multipart/form-data" in form tag.

<form method="POST" action="apply" class="form-horizontal custm-form" role="form" enctype="multipart/form-data">
2 likes
MrRobot21's avatar

@Frankiec91 Wow Thank you so much it's working, You are super cool

but i have a small problem i guess

The file is uploading into the "public/uploads/" which is super cool

The link in the database is not as expected, The table name is "avo_career" and the column name is "resume" and

The path of the file storing is

C:\wamp\tmp\php11A6.tmp

It Should be as following right

public/uploads/fiename.pdf

Thanks

yulquen's avatar

You have to set the column value to the new filename value as well:

// ...
$avoCareer->resume = $fileName;
$avoCareer->save();
1 like
MrRobot21's avatar

@rephluX , @Frankiec91 Thank you for your relpy

i tried but the path storing in the table is the same, column name is "resume_link"

C:\wamp\tmp\php4D5F.tmp

And My Controller is "AvoCareersController.php"

<?php 

namespace App\Http\Controllers;

use View;
use App\AvoCareer;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Redirect;
use Illuminate\Routing\Controller as BaseController;

class AvoCareersController extends BaseController {

    public function careerpage() {

        return view('careers.apply');
    }

    public function store(Request $request)
    {

        $avoCareer = new AvoCareer;
        $avoCareer->position     = $request->position;
        $avoCareer->name         = $request->name;
        $avoCareer->email        = $request->email;
        $avoCareer->contact_num  = $request->contact_num;
        $avoCareer->current_city = $request->current_city;
        $avoCareer->year         = $request->year;
        $avoCareer->month        = $request->month;
        $avoCareer->location     = $request->location;
        $avoCareer->resume_link  = $request->resume_link;
        $avoCareer->save();

        $fileName = $avoCareer->id . '.' .
        $request->file('resume_link')->getClientOriginalExtension();

        $request->file('resume_link')->move(
            base_path() . '/public/uploads', $fileName
        );
     
        $avoCareer->resume_link  = $fileName;
        
        return redirect('apply');
    
    }

}


?>

Thank You

Vijaykj's avatar

I'm able to delete resume_link path in the column using update state by passing Null value and how I can delete the file from the folder path. Please any guide to resolve it.

Please or to participate in this conversation.