【问题标题】:using php file handling in laravel 4.2在 laravel 4.2 中使用 php 文件处理
【发布时间】:2016-05-11 03:07:24
【问题描述】:

嗨,我在使用文件处理方面非常新,所以我想做的是打开一个 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

关于我做错了什么的任何想法?或者有什么想法可以改善我的逻辑?非常感谢

【问题讨论】:

    标签: php laravel laravel-4 file-handling


    【解决方案1】:

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

    替换这一行:

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

    用这个:

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

    【讨论】:

    • 非常感谢!
    【解决方案2】:

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

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

    【讨论】:

    • asset 在这个例子中不好。为什么?脚本试图访问 LOCAL 文件,但使用 REMOTE 路由。打开另一个连接并调用 Web 服务应用程序有什么意义?正确的方法是在这种情况下使用public_path。当您不运行 Web 服务时,它也有助于测试。
    猜你喜欢
    • 1970-01-01
    • 2023-03-21
    • 2015-09-09
    • 2017-04-08
    • 2010-09-30
    • 1970-01-01
    • 1970-01-01
    • 2014-05-10
    • 2015-12-29
    相关资源
    最近更新 更多