BourneShady
BourneShady

Reputation: 935

using php file handling in laravel 4.2

hi im pretty new in using file handling so what im trying to do is open a docx file then put it into a string that i will be displaying in my view. I have read that PHP file handling is already in the core of php so i wanted to try to integrate it into laravel 4.2 so here is my controller

public function open($id)
{
    $title = "Open files";

`

    $smpl = DB::table('sfiles')
            ->where('fileid' , '=' , $id)
            ->get();

    foreach($smpl as $file)

    $fname = $file->filename;

    $opendoc = file_get_contents('public/upload/docs/' . $fname);

    return View::make('dmy.open_doc' , compact('title', 'smpl'));
}`

when executed , im having an error saying

file_get_contents(public/upload/docs/2016-05-11-10-09-55-ppd-jobdesc.docx): failed to open stream: No such file or directory

any ideas on what iam doing wrong? or any ideas to improve my logic? thanks so much

Upvotes: 0

Views: 241

Answers (2)

Filip Koblański
Filip Koblański

Reputation: 9988

You need to use asset function that will point a path to your public asset like this:

$opendoc = file_get_contents(asset('upload/docs/' . $fname));

Upvotes: 1

Giedrius Kiršys
Giedrius Kiršys

Reputation: 5324

You are trying to access relative path. You have to use absolute path.

Replace this line:

$opendoc = file_get_contents('public/upload/docs/' . $fname);

with this one:

$opendoc = file_get_contents(public_path('upload/docs/' . $fname));

Upvotes: 2

Related Questions