upload custom directory plugin - move images

11 posts by 3 authors in: Forums > CMS Builder
Last Post: December 30, 2022   (RSS)

By kitsguru - December 10, 2022

I am working on a plugin to move files from the system upload directory to a custom directory for each table.

I was looking for the functions that determine which directory to use when uploading but could not find it. Any help on this is appreciated.

  1. My plugin will produce a list of schemas with their upload directories. the user will select which schema/upload fields to process.
  2. It will then get an array of files from the uploads table for that table/field
  3. It will check to see if the file exists in the upload directory and not the custom directory.
  4. then move the file and all thumbnails to appropriate custom directory
  5. Since the path is not stored in the uploads table, this does not require an update.

I couple things I will need to check and clean up along the way.

  • Some older versions stored the path in the uploads table
  • File names may not be unique across tables with system and custom directories in use

Once I have this plugin done, I will donate it to the community. 

If anyone has any thoughts or suggestions, I would like to hear them.

Jeff Shields

By Dave - December 22, 2022

Hi Jeff, 

Sorry for the delay.  Did you figure this out yet?  If you're still working on it check out this function in /lib/upload_functions.php

// Returns the absolute uploadDir and uploadUrl for a specific upload field. This is determined by: The dir/url of the CMS,
// .. the (potentially relative) upload dir/url in settings, and the (potentially defined) custom upload dir/url for the field.
// Usage: list($uploadDir, $uploadUrl) = getUploadDirAndUrl($fieldSchema);
function getUploadDirAndUrl($fieldSchema, $settingsUploadDir = null, $settingsUploadUrl = null, $returnOnErrors = false) {

It'll get you the full path and URL for uploads.

Let me know if you have any other questions.  Thanks!

Dave Edis - Senior Developer
interactivetools.com

By kitsguru - December 23, 2022

That is what I was looking for, thanks Dave.

But I do have a question. It would be good if the plugin could hook the section editor to detect a change in the custom directory and provide the option to move files right then and there. Is there a way to hook the section editor?

Right now my issue is there is no way to detect that the custom directory has been changed and now using the settings['uploadDir'] or new custom directory. 

Any thoughts appreciated.

Jeff Shields

By Dave - December 30, 2022

Hi Jeff, 

What's the end objective or problem you're trying to solve?  

If it's to allow the users to change the custom upload folders you could have a plugin that runs when that folder is changed.  Or a general plugin that scans the upload table and checks all the files.  If they're not in the expected folder it could look for them in the default folder and move them.

Let me know some more details and I'll try to help.

Thanks!

Dave Edis - Senior Developer
interactivetools.com

By kitsguru - December 30, 2022

The end objective of my plugin is to move files to the appropriate directories when the custom directory is changed in the section editor.

I have my plugin working by manually entering old directory and new directory in AdminUI.

What plugin hook should I use to trigger it when saving the change? Is it:

addAction('section_preDispatch', 'moveFileOnCustomDirectorChange');
Jeff Shields

By kitsguru - December 30, 2022

I just figured it out

Jeff Shields

By gkornbluth - December 30, 2022

Hey Jeff,

Care to share?

Happy New Year!

Jerry Kornbluth

The first CMS Builder reference book is now available on-line!







Take advantage of a free 3 month trial subscription, only for CMSB users, at: http://www.thecmsbcookbook.com/trial.php

By kitsguru - December 30, 2022

Once I have it ready, I will donate it to the community

Jeff Shields

By gkornbluth - December 30, 2022 - edited: December 30, 2022

Appreciate it,

Sounds like a useful plugin.

I'm sorry that the some of the user submitted plugins have disappeared from the IT site.

I'm going to go through the ones I have and make sure to add them to the CMSB Cookbook Site as well

Jerry Kornbluth

The first CMS Builder reference book is now available on-line!







Take advantage of a free 3 month trial subscription, only for CMSB users, at: http://www.thecmsbcookbook.com/trial.php

By Dave - December 30, 2022

Hi Jerry, 

>I'm sorry that the some of the user submitted plugins have disappeared from the IT site.

You can find the user-submitted ones here.  If there's any you want that we don't have let me know.

https://www.interactivetools.com/plugins/?User-Submitted-9

Thanks!

Dave Edis - Senior Developer
interactivetools.com