Jump to content

Is there a way to upload files or images to custom directories in the assets folder?


landitus
 Share

Recommended Posts

Hi all! I was wondering if it's possible to set up custom destination directories for different templates and/or fields. The example would be the following:

I have a custom field named 'pdfs' and everything it uploads goes to /assets/pdfs. And so on? You know what I mean? So the file url would be cleaner and meaningful. This way I would have some url organization for files, like: assets/brochures, or assets/pdfs.

Thanks in advance!

Link to comment
Share on other sites

This isn't possible using the default file and image fieldtypes, though anything is possible when making your own fieldtypes. A workaround that may be possible is to create a page called 'files' and set it's template ('files-gateway') to have URL segments ON. The page/template could then execute a search for the file (in field 'files') and redirect to it, or perform a passthrough. So it'd work something like this:

Page: /pdfs/ run by template 'files-gateway' with this code in the template file:

<?php
if(!$input->urlSegment1) throw new WireException("No file specified");
$filename = $sanitizer->pageName($input->urlSegment1);
$p = $pages->get("files=$filename");
if(!$p->id || !$p->viewable()) throw new Wire404Exception("File not found");
$session->redirect($p->files->get($filename)->url); // redirect to it

So an access to /pdfs/my-file.pdf would redirect to the right file. You could also perform a passthrough, where you literally feed the file to them right here, but that's a little less efficient.

Link to comment
Share on other sites

Thanks Ryan! I will try to look into it. I appreciate the example. I'll let you know. Do you think it would be useful to associate a file upload field to a path in assets folder, in future releases? Do you it would be a feature worth considering?

Link to comment
Share on other sites

This isn't something that's on the roadmap. URLs of file-based assets are usually something that's behind the scenes and not something that the user or the administrator typically interacts with directly. We use this to our advantage and ensure that files are stored in a manner that is organized, can scale, limits potential name conflicts, and relate to their source. This ensures the CMS can effectively manage them with a page, avoid file system limitations, and recover from errors. I would see a common directory like /pdfs/ as being more the forte of something like a media manager that exists independently of pages. And that may be something that we'll see as a 3rd party module at some point.

Link to comment
Share on other sites

Yes! I think the idea of a file manager would be useful and has it's uses. It can re-use a file without duplicating it, and some more. I think expression engine has this, btw. I'll stick to a special downloads page and relate to to some other entry to manage available download files.

Thanks!

Link to comment
Share on other sites

It can re-use a file without duplicating it, and some more.

ProcessWire will let you do this too. You can pull any file/image from any page, whether in the editor or in the API. When I need a lot of shared images, I usually setup a /tools/library/ page or something like it to serve as an image/file store for shared assets. Pages have a lot of uses. :)

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...