可以通过以下代码完成相同的工作:
header('Content-Type:image/jpeg');
readfile('a.jpg');
但现在我真的对 Yii2 感到困惑\yii\web\Response.
我的困惑是这样的:
-
创建一个控制器和动作来提供图片
见下文
class ServerController extends \yii\web\Controller
{
public function actionIndex($name)
{
// how to response
}
}
access http://example.com/index.php?r=server/index&name=foo.jpg
感谢你的回答!
最后,我通过以下代码完成了:
$response = Yii::$app->getResponse();
$response->headers->set('Content-Type', 'image/jpeg');
$response->format = Response::FORMAT_RAW;
if ( !is_resource($response->stream = fopen($imgFullPath, 'r')) ) {
throw new \yii\web\ServerErrorHttpException('file access failed: permission deny');
}
return $response->send();
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)