gpt4 book ai didi

php - Symfony2 中的私有(private)图像

转载 作者:行者123 更新时间:2023-12-02 06:59:47 27 4
gpt4 key购买 nike

我的 Symfony 安装有点问题。在我的应用程序中,用户可以将图像上传到服务器,图像存储在/web/uploads/images 中。我希望该图像仅对已登录应用的用户可见。

我已尝试修改安全配置,但找不到解决问题的正确配置。有什么想法吗??

谢谢:)

最佳答案

首先非常感谢您花时间回答:)我认为这两种解决方案都是有效的。在我的特殊情况下,我已经实现了@Patrick 提出的解决方案。

很快我将需要通过 url 从移动应用程序访问私有(private)图像,因此出于这个原因,我实现了一个 Controller 而不是 twig 扩展, Controller 的代码如下:

<?php

namespace Company\Bundle\nameBundle\Controller;

use Symfony\Component\HttpFoundation\Response,
Symfony\Bundle\FrameworkBundle\Controller\Controller;

class DashPrivateImageController extends Controller
{
public function getImageAction($imagetype, $imagename) {

$securityContext = $this->container->get('security.context');

if ($securityContext->isGranted('IS_AUTHENTICATED_REMEMBERED')) {

if ($imagetype == 1)
$path = $this->get('kernel')->getRootDir() . "/../private/documentFolder1/" . $imagename;
else
$path = $this->get('kernel')->getRootDir() . "/../private/documentFolder2/" . $imagename;

$file = file_get_contents($path);
$headers = array('Content-Type' => 'image/jpeg',
'Content-Disposition' => 'inline; filename="'.$imagename.'"');
return new Response($file, 200, $headers);
} else
return new Response("not found", 404);
}
}
?>

谢谢!!

关于php - Symfony2 中的私有(private)图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23252897/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com