Nikhilesh Sharma
Nikhilesh Sharma

Reputation: 151

FileNotFoundException in File.php line 41

In laravel i am making an application that uploads a file and the user can download that same file.

But each time i click to download i get this error. enter image description here

The view code:

<h5><a href="/download/{{$filesharing->fileName}}/{{$filesharing->fileType}}">Download</a></h5>

The route code:

Route::get('/download/{fileName}/{fileType}', 'FilesharingsController@download');

The controller code:

public function download($fileName, $fileType){
    $downloadPath = public_path(). '/assests/' . $fileName ;
    $headers =  array(
        'Content-Type: application/octat-stream',
        'Content-Type: application/pdf'
    );

    return Response::download($downloadPath, $fileName . '.' . $fileType, $headers);
}

Please not that when i upload a file i remove its extension. Example: if i upload 'sample.pdf' it is saved as 'sample'.

I have no clue what is wrong as the path in the error is the correct path. And the file exists in that path. Plz help

The folder Structure: enter image description here

And the code used to upload the file is:

// Save uploaded file
    if ($this->request->hasFile('file') && $this->request->file('file')->isValid()) {
        $destinationPath = public_path() . '/assests/';
        $fileName = $filesharing->fileName . '.' . $this->request->file('file')->guessClientExtension();
        $this->request->file('file')->move($destinationPath, $fileName);
    }

Upvotes: 2

Views: 3018

Answers (1)

Tim
Tim

Reputation: 5943

Your $downloadPath is missing the file extension. The second parameter of Response::download is the file name shown to the user.

Your variable should look like this:

$downloadPath = public_path() . '/assets' . $fileName . '.' . $fileType;

Upvotes: 1

Related Questions