Credit to Donovan Schönknecht for his Amazon S3 PHP class.
This snippet will send files uploaded to a Gravity Forms form to your Amazon S3 account. To get started, first download the files. Then, some customization steps are required before use:
- On line 12, replace
access-key-here
with the Access Key provided to you when you registered for your S3 account. - On line 13, replace
secret-key-here
with the Secret Key provided to you when you registered. - On line 14, replace
bucket-name
with the name of the bucket you wish to upload the files to. - On line 17, replace
1
with the ID of the form that will be sending files to S3. - On line 18, replace
18
with the ID of the File Upload field.
Also, note that this does not prevent the uploaded files from being stored on your server. If you wish for the files to not be stored locally, I’m not aware of anything within Gravity Forms that makes it possible so you’ll need to use the PHP unlink() function.
For a neat tutorial on renaming Gravity Forms file uploads, check out this article from David at GravityWiz.com. You can use his code with the code from this tutorial to rename the files prior to sending them to S3 so to maintain a specific naming scheme and prevent overwrites.
Ren says
Since this is a bit outdated, I’d recommend a better solution for it now that handles things like deleting files from your server, supports multiple accounts/buckets, and bulk processing past file uploads.
https://elegantmodules.com/modules/gravitys3/
Laurent Fourcade says
The plugin doesn’t work. I bought a licence from them. The support is gone. No answser to my messages after 1 month. A shame. You should put a note on this page Ren to inform people about the situation.
Filip says
+1 we had exactly the same experience
Mike says
Thanks for this! Does this handle updates to the file uploads and deletions?
mohd ashraf says
hi
does the files go to my server then s3 bucket or directly to s3 bucket without disturbing my server.
Paul says
Hi Ren,
I try to use your code to put file to S3 but it does not go to. It need to go to if clause but it always goes to else. Do you have any idea about this problem?
Thank you.
Ren Ventura says
Hi, Paul. Without seeing your code, it’s hard to tell what may be going on. My first though is that it may be your authentication keys aren’t correct.
Daniel says
Hallo,
where to upload the files?
“Send Gravity Forms File Uploads to Amazon”
regards
Daniel
Riz M says
I am not quite sure on the unlink() function. Could you please tell me how to do this? I’ve been searching the answer for months (been searching for over 2 years) would love to know your answer on how can you upload files to AmazonS3 without having to store it on the server.
Philip says
Quick question – What if I have multiple forms – I assume I would do something like
define( ‘FORM_ID’, array(ID1, ID2, ID3));
and same for define( ‘FILE_UPLOAD_FIELD_ID’, 18 );
If you could confirm thanks
ramkumar says
does this work with large file size???
Asher says
How can I retrieve the link of the uploaded file from S3?
Nikole Gipps says
What if you have multiple form IDs that you are using for this?
509tyler says
I successfully set this up on a dev site – works well and used the Gravity Wiz file-rename – very cool. Then moved the plugin to my LIVE multisite installation, installed and activated the custom plugin on the subsite but it seems maybe the path to the files on the multisite installation is not being read correctly as the files from the uploaded form on the sub site are not arriving on S3.
Any suggestions?
Nick says
Awesome tips! Everything works great, but one question. What do I need to add to store the file in a subfolder inside a S3 bucket (instead of the main bucket)? Say the bucket’s name is “bucket-name” and there is a subfolder inside called “target-folder” where I want to files to go. Thanks for all the help!
Erik says
I believe I’ve taken the correct steps to set this up, however upon submission my form hangs and does not continue to the confirmation page. I enabled WP_DEBUG logging and noticed the following error:
[30-Apr-2015 19:57:25 UTC] PHP Warning: S3::putObject(): [MalformedXML] The XML you provided was not well-formed or did not validate against our published schema in /wp-content/plugins/gfs3/inc/S3.php on line 363
[30-Apr-2015 19:57:25 UTC] PHP Warning: S3::putObject(): [MalformedXML] The XML you provided was not well-formed or did not validate against our published schema in /wp-content/plugins/gfs3/inc/S3.php on line 363
Any idea what might be the issue here? Thanks in advance for any insight.
Ren says
I can’t think of anything right now. Without seeing your code, though, it would be hard to tell.
Erik says
Thank you for sharing this code. So the snippet goes in your functions.php, and where does the /inc folder go within the WordPress folder structure? I’m a bit confused about where to insert the code/files you’ve provided.
Ren says
Hello, Erik. This type of functionality is best to put into a plugin. Theme files (i.e. functions.php) are intended for presentation and everything else should be in a plugin. This is because you’ll lose the functionality if you switch themes. Here’s a link on how to build a plugin for it.
Once you create the plugin and the main file, just add a folder called “inc” and add the S3.php file to that.
Erik says
Great, exactly the information I was looking for. Thanks for the quick reply.
Ren says
You’re welcome!