我试图通过使用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
该文件存在于路径中,但我不知道为什么会出现此错误。
这些答案对我帮助不大。
第一个
第二个
尝试使用绝对url
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);
如果您确定文件Url或路径正确,请检查文件权限。
file_get_contents(dirname(__DIR__).'/../web/files/upload/1.txt',true);
如果您的文件存在'/yourproject/web/filess/upload/1.txt'