Symfony-从请求中存储图像

发布于 2025-02-12 01:30:14 字数 427 浏览 1 评论 0原文

我正在制作具有添加帖子功能的应用程序。每个帖子都有可能包含图像。我的主要问题是我不知道如何存储图像。我知道我可以创建一个名为“私有”的单独文件夹,但事实是,它应该具有一定的结构来不将所有图像放在一起。我想避免在一个文件夹中拥有数千张图像。我在某个地方看到了人们制作文件夹结构的地方,然后以某种方式决定应存储每个图像的位置(例如,用原始文件名)。这看起来像这样:

private:
    15:
       23:
          image_file.jpg
       88:
          image_file2.jpg
    13:
       48:
          image_file3.jpg

有没有办法在Symfony中执行这种文件结构?我唯一的解决方案是以某种方式将文件名转换为数字,然后使用Modulo。但是我认为这不是一个好主意。

I am making an app with a feature of adding posts. Every post have the possibility to contain an image. My main problem is the thing I don't know how to store the images. I know I can create a seperate folder named for example "private" but the thing is it should have some structure to not put all images together. I would like to avoid having thousands of images in one folder. I've seen somewhere that people make a structure of folders then somehow decide where each image should be stored (for example by original file name). This looks something like this:

private:
    15:
       23:
          image_file.jpg
       88:
          image_file2.jpg
    13:
       48:
          image_file3.jpg

Is there a way to do this kind of file structure in Symfony? The only solution that I have in my mind is somehow transforming filenames into numbers then using modulo. But I don't think it is a good idea.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

云裳 2025-02-19 01:30:14

只需将您的帖子ID纳入路径:

use Symfony\Component\HttpFoundation\File\UploadedFile;
use Entity\Post;

class ArticleAdminController extends BaseController
{
    public function temporaryUploadAction(Request $request, Post $post, string $projectDir)
    {
        /** @var UploadedFile $uploadedFile */
        $uploadedFile = $request->files->get('image');
        $uploadedFile->move("{$projectDir}/private/{$post->getId()}");
    }
}

Simply incorporate your Post ID into path:

use Symfony\Component\HttpFoundation\File\UploadedFile;
use Entity\Post;

class ArticleAdminController extends BaseController
{
    public function temporaryUploadAction(Request $request, Post $post, string $projectDir)
    {
        /** @var UploadedFile $uploadedFile */
        $uploadedFile = $request->files->get('image');
        $uploadedFile->move("{$projectDir}/private/{$post->getId()}");
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文