【问题标题】:Error failed to open stream: No such file or directory in file_get_contents()错误无法打开流:file_get_contents() 中没有这样的文件或目录
【发布时间】:2016-12-20 21:08:43
【问题描述】:

我正在尝试使用 file_get_contents() 函数显示文本文件 (1.txt) 的内容,当然我正在使用 yii2 框架。

如果yii2有更简单的功能请给我推荐。

这是我在控制器中的代码:

public function actionRandom() {

        $fileContent = file_get_contents(Yii::$app->request->baseUrl.'/files/upload/1.txt',true);
        echo Json::encode($fileContent);
        Yii::app()->end();

        return $this->render('random', [
                        ]
        );
    }

结果中出现此错误。

file_get_contents(/test/files/upload/1.txt): failed to open stream: No such file or directory

该文件存在于路径中,但我不知道为什么会出现此错误。

这些答案对我帮助不大。

first

second

【问题讨论】:

标签: php yii2 yii2-advanced-app


【解决方案1】:
file_get_contents(dirname(__DIR__).'/../web/files/upload/1.txt',true);

如果您的文件存在 '/yourproject/web/files/upload/1.txt' 将有效

【讨论】:

    【解决方案2】:

    尝试使用绝对网址

    use yii\helpers\Url
    
    Url::to(Yii::$app->request->baseUrl  .'/files/upload/1.txt' , true);
    $fileContent = file_get_contents(Url::to(Yii::$app->request->baseUrl  
             .'/files/upload/1.txt' , true),true);
    

    【讨论】:

    • 我想将文件的内容放在一个变量中。不链接到那个。
    • 答案应该为 file_get_contents() 提供正确的链接,然后 file_get:contents 应该在 $fileContent 中返回文件的内容
    【解决方案3】:

    如果你确定文件的 URL 或路径是正确的,那么检查文件的权限。

    【讨论】:

    • 我该怎么做?
    • sudo chmod 775 文件名
    • 我无法理解。请解释更多。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-25
    • 2019-07-27
    • 2013-12-15
    • 2014-03-02
    • 2017-06-20
    相关资源
    最近更新 更多