Your cart is currently empty!
Programatically upload file to Groups Protected Files
—
by
Is it possible to upload a file to groups files programatically?
I want to make it easier for my client to create a post and have the files they upload protected. I was going to use ACF field type to upload this file then on post save, take that file and ‘send’ it to groups files and get back the id to create the download link.
I can hook into save post and get the file from ACF easily and looking at the docs I can get the id once the groups file is created using groups_created_file. Can I somehow send the file from ACF (which will upload it to the uploads directory) and send it to groups files?
Thanks
Posted in Groups File Access
Comments
10 responses to “Programatically upload file to Groups Protected Files”
Yes Patrick,
Accessing the file_id property is restricted, but you can get the id with get_file_id() method.
Cheers
Hey Patrick,
That’s great news, I’m glad we could help you with your project.
FYI, the method for reading stored file data needs the file id to be passed, perhaps this is why it is returning null. Why don’t you give it a try…
Kind regards,
George
Hi George, yes it’s getting the ID that’s the issue. I’ve created the instance with the data (from ACF) but have to use null for the ID as i can’t know what it is. Once I then create the actual file with $protected_file->create(); I can step through and see that the file has been created, moved across and has an ID but I can’t access it as it’s a private property. e.g.
$protected_file->file_id comes up as accessing a private property.
https://www.dropbox.com/s/8k3cdgn25g8cl66/CleanShot%202022-11-22%20at%2009.33.29.png?dl=0
Hey Patrick,
Just in case you haven’t noticed, we have updated the documentation for Groups File Access plugin and I think you will find all the necessary details for your request under:
– https://docs.itthinx.com/document/groups-file-access/api/classes/
However, if further assistance is needed you can always follow-up here.
Kind regards,
George
Hi George, thanks for the new GFA_File class. I’ve got everything nearly working I’m just having an issue accessing the file_id of the newly created file. On post save I get the file from ACF and create a new GFA_File, upload it, everything works. I just can’t access that new ID to save into another custom field.
I’m using the class
$protected_file=new GFA_File(
null,
[
‘source’ => $file_array,
‘filename’ => ‘test-to-protect-‘ . date( ‘Y-m-d-H-i-s’ ) . ‘.pdf’,
‘name’ => $file_name,
‘group_ids’ => [ BUSINESS_GROUPS_ID ],
]
);
$protected_file->create();
but $protected_file->read() is coming up as null.
The file is being created correctly and everything else works. Any ideas on how to access that ID?
Hey Patrick,
That’s correct, we’ve released an updated version of the plugin that includes an extended API. As we speak we are also preparing the update in the documentation so IMO you won’t need to reverse engineer the functionality.
Kind regards,
George
Hey Patrick,
Adding the file to the protected folder is only one part of this approach and the rest of it requires some additional API functions that we are working on. Once this is prepared, I’ll let you know exactly what you should use.
Cheers
Hi George, thanks for the quick response. Yes, I thought I’d have to move the file out of the general uploads folder to the protected downloads folder but that doesn’t automatically add it to groups files and doesn’t give me an ID to use in the post meta does it? Is there anyway of doing this?
Looking at the plugin would it be possible to call import_files() or gfa_admin_files_add_submit() from within a separate plugin and somehow post the correct info and get back the file ID?
Thanks
Hi Patrick,
Perhaps you should try and upload the file to the GFA dedicated folder uploads/groups-file-access/ because there, any uploaded file is protected by the plugins .htaccess rule and because this is the only location where the plugin looks and serves files from.
We are already checking new ways to extend the plugin API with REST but also other ways of including more folders in the restriction “zone”, but at the moment I haven’t got an exact ETA for these features.
Kind regards,
George
Hi George, I’ve just seen there was an update to add the new GFA_File class – is this the API you were talking about? If so, thanks, that was really quick. Do you have any documentation on this? Trying to work it out reverse engineering the class and can’t quite figure it out! Thanks