Simplify your stack and build anything. Or everything.
Build tomorrow’s web with a modern solution you truly own.
Code-based nature means you can build on top of it to power anything.
It’s time to take back your content infrastructure.

Organize media files by subfolders

default discord avatar
mihanc2 years ago
2

Hi there! Does anyone know a workaround on how to organize media files by subfolders?


I want to create an admin panel for a blog where authors will work on writing articles. Each article may have at least 5-10 pictures. If all of them are uploaded to a media folder without sorting by subfolders, I'll get a mess...


Thanks in advance!

  • default discord avatar
    thgh.2 years ago

    Here is a way to prefix the filename (not sure if folders are supported)


    // Field
    {
      name: 'original',
      label: 'Original filename',
      type: 'text',
      admin: { hidden: true },
    },
    // Hook
    beforeOperation: [
      async ({ operation, args }) => {
        if (operation === 'create') {
          // Save original filename
          const original = args.req.files.file.name || 'example.jpg'
          args.data.original = original
    
          // Add filename prefix
          args.req.files.file.name = new Date().toJSON().slice(0, 10) + '-' + original
        }
      },
    ],
  • default discord avatar
    jessrynkar2 years ago

    Hey

    @187948379920138240

    - you could also organize your media by adding a tag field or similar, then you can filter by this tag in your collection view.

Star on GitHub

Star

Chat on Discord

Discord

online

Can't find what you're looking for?

Get dedicated engineering support directly from the Payload team.