Maintaining the Directory Structure of Files Uploaded via UploadField

Silverstripe Version: 3.1

Hello,

I’m using an UploadField on a page that allows multiple file uploads.

I would like the files uploaded by this field to maintain their directory structure within the upload directory, for example, if the file being uploaded is /DirectoryA/DirectoryB/File.pdf, I would like it to be stored on the server as /Uploads/DirectoryA/DirectoryB/File.pdf rather than /Uploads/File.pdf.

When the files are selected in the UploadField, these directory structures are listed prior to the submission of the form, so I know that the information is being obtained by the UploadField, but I’m not sure how to modify the upload in such a way that these directory structures are maintained.

Although I’m using an older version of SilverStripe, I am willing to upgrade if a solution exists in a newer version that isn’t possible using the current version; as such, please feel free to give responses that are dependent on SilverStripe 4.0+.

Best regards and thanks in advance.

Want to make sure I understand your question right - in your example is ‘/DirectoryA/DirectoryB/’ a path on your computer? It probably goes without saying but this would be an undesirable behaviour for most projects, so I doubt there would be an existing module for this. I also didn’t think browsers/javascript could read the local file path of an upload (I would hope they couldn’t) so if you’re seeing the full path I’m guessing you’re using SS2 and the file path is displayed in a native browser-rendered file field, rather than a custom upload widget. So if I understand your question properly, I don’t think it’s possible to do what you want to do.

If you’re using SS2 or SS3 then an alternative option might be to use an FTP app with a sync feature (like Panic’s Transmit). You should then be able to select a directory on your computer and tell the FTP app that it is equivalent to the assets directory on your web server, so any uploads you do through there are uploaded to the same relative location. After uploading via FTP you can do a file sync in the CMS to make the CMS aware of those new files. This wouldn’t work in SS4 as the file sync feature has been removed.

Thank you for your response,

Well, to be a bit more specific, I’ve modified the UploadField slightly to accommodate the submission of entire directories. As an example of a use case, the client wants to upload a directory containing sub-directories that divide their files into groups based on their organisation scheme. Their intention is to submit a folder, which may be Directory A with subdirectories B and C, and have the relative path from the folder be reflected in how it’s stored on the server.

So, I guess to rephrase–if the contents of one folder are submitted to an UploadField permitting multiple files, what I’d like to be able to do is maintain the sub-directories of that folder as part of the upload’s structure.

Thanks again.

Ah okay, that makes more sense. Can you post the modified code? I’m guessing you’re looping over the files and folders in the selected directories, and I assume you can read or build a relative path to each file as you’re doing that. If you can do that, you should be able to modify the form post to include the relative paths in an additional field, or perhaps you can send them as pairs of file + path. On the PHP side you would need to modify the form structure to allow for that extra information, and overwrite the upload action to read the relative path and store it in the appropriate place.