我可以在yii2的后端web文件夹外显示图片吗?
我有个小问题。我无法查看后端web文件夹之外的图像 common\config\main中的别名:我可以在yii2的后端web文件夹外显示图片吗?,yii2,Yii2,我有个小问题。我无法查看后端web文件夹之外的图像 common\config\main中的别名: 'aliases' => [ '@upload' => dirname(dirname(__DIR__)).'/upload', ], 查看数据提供程序: [ 'format' => 'raw', 'label' => 'Immagine', 'value' => function ($data) { ret
'aliases' => [
'@upload' => dirname(dirname(__DIR__)).'/upload',
],
查看数据提供程序:
[
'format' => 'raw',
'label' => 'Immagine',
'value' => function ($data) {
return Html::img(Yii::getAlias('@upload') . $data->codice_prodotto . '/' . $data->immagine, ['width' => '70px', 'class' => 'img-thumbnail']);
},
],
我能解决吗?谢谢。如果您的文件无法通过http服务器访问,则无法直接下载 您可以:
- 将上载目录移动到http服务器可访问的目录
- 创建将从私有目录读取文件并将其流式传输到浏览器的操作(可以使用yii\web\Response::sendFile()函数)
public function actionFile($filename)
{
$storagePath = Yii::getAlias('@upload');
// check filename for allowed chars (do not allow ../ to avoid security issue: downloading arbitrary files)
if (!preg_match('/^[a-z0-9]+\.[a-z0-9]+$/i', $filename) || !is_file("$storagePath/$filename")) {
throw new \yii\web\NotFoundHttpException('The file does not exists.');
}
return Yii::$app->response->sendFile("$storagePath/$filename", $filename);
}
[
'format' => 'raw',
'label' => 'Immagine',
'value' => function ($data) {
return Html::img(Url::to(['/path/to-streaming-action/file', 'filename' => $data->codice_prodotto . '/' . $data->immagine]), ['width' => '70px', 'class' => 'img-thumbnail']);
},
],
并查看数据提供程序配置:*
public function actionFile($filename)
{
$storagePath = Yii::getAlias('@upload');
// check filename for allowed chars (do not allow ../ to avoid security issue: downloading arbitrary files)
if (!preg_match('/^[a-z0-9]+\.[a-z0-9]+$/i', $filename) || !is_file("$storagePath/$filename")) {
throw new \yii\web\NotFoundHttpException('The file does not exists.');
}
return Yii::$app->response->sendFile("$storagePath/$filename", $filename);
}
[
'format' => 'raw',
'label' => 'Immagine',
'value' => function ($data) {
return Html::img(Url::to(['/path/to-streaming-action/file', 'filename' => $data->codice_prodotto . '/' . $data->immagine]), ['width' => '70px', 'class' => 'img-thumbnail']);
},
],
*请注意,此代码尚未准备好复制粘贴,请仔细阅读并尝试理解其原理,然后再在代码中实现它。您从
Yii::getAlias('@upload')获得什么url$数据->密码(prodotto./)$data->immagine
?或者您可以使用类似以下内容的yii\helpers\Url::to(['@backend/web/upload'])
简单的解决方案也可能是创建一个符号链接以上传文件夹,以便直接访问该文件夹,例如在backend/web
中。