在 laravel 4.2 中使用 php 文件处理

using php file handling in laravel 4.2

嗨,我在使用文件处理方面还很陌生,所以我想做的是打开一个 docx 文件,然后将其放入我将在我的视图中显示的字符串中。我读到 PHP 文件处理已经在 php 的核心中,所以我想尝试将它集成到 laravel 4.2 所以这是我的控制器

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'));
}`

执行时,我有一个错误说

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

关于我做错了什么有什么想法吗?或任何改进我的逻辑的想法?非常感谢

您正在尝试访问相对路径。你必须使用绝对路径。

替换此行:

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

这个:

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

您需要使用 asset 函数来指向您的 public 资产的路径,如下所示:

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