Jordan Lev Logo Jordan Lev's Concrete5 Tips

How to make things work when building a Concrete5 website.

File Upload Attribute for User Registration

| Comments

A project I worked on recently required that users upload a file when they register for the site. I thought the built-in “Image/File” attribute type would be able to handle this, but I was wrong. The problem is that it only provides a File Manager selector, not an actual HTML file input tag. Even if I wanted to let the general public see the File Manager (which I don’t), it wouldn’t work because these are unregistered people who don’t have access to the File Manager anyway.

So I created my own attribute type for this, and am sharing it here.

file_upload_attributes.zip

Read on for installation instructions, usage details, and caveats/limitations…

Installation

To install it, unzip it and move the file_upload_attribute folder into your site’s top-level packages directory, then go to Dashboard -> Add Functionality and click the “Install” button next to “File Upload Attribute” over on the right. The attribute type will associate itself with the Users category upon installation, but you’ll need to actually create an attribute of this type (via Dashboard -> Users and Groups -> Attributes) before it will appear on your registration form.

What It Does

Right now it works but is very simple. Files are uploaded to a sub-directory of your site’s “files” directory, so it completely bypasses the File Manager. This means you can’t view the files in the File Manager, but you can see a link to the file in the User’s dashboard page (just like all custom user attributes). And if you have registration notification emails enabled (by defining the EMAIL_ADDRESS_REGISTER_NOTIFICATION constant in config/site.php), a link to the file will also be included in the notification email.

If you want this link to be “clickable” in the email, you will need to make the email notification template include HTML in addition to plain-text, which you can do by copying SITEROOT/concrete/mail/user_register_approval_required.php and SITEROOT/concrete/mail/user_register.php to SITEROOT/mail/user_register_approval_required.php and SITEROOT/mail/user_register.php (respectively), and then adding $bodyHTML = nl2br($body); to the bottom of both files.

Important Notes and Limitations

  • File uploads never work in HTML unless you specify enctype="multipart/form-data" in your <form> tag. You’ll need to copy SITEROOT/concrete/single_pages/register.php to SITEROOT/single_pages/register.php, edit that file, and add enctype="multipart/form-data" to the <form> tag there.
  • You can change the name of the directory it uploads files to via the $upload_dir_name variable at the top of SITEROOT/packages/file_upload_attribute/models/attributes/types/file_upload/controller.php
  • You can optionally restrict the allowed file types by adding file extensions (without dots) to the $allowed_extensions array at the top of SITEROOT/packages/file_upload_attribute/models/attributes/types/file_upload/controller.php.
  • Due to limitations of the attribute system in Concrete5, when a validation error occurs, the same message is always displayed (“file is required”), regardless of what the actual error was. So if the upload fails because the file is of the wrong type, users will still see the “file is required” message. Bummer :(
  • I haven’t yet implemented any file deletion functionality, so if you have a lot of people registering for your site, you’ll want to keep an eye on the uploads directory to make sure it doesn’t get too big. (I hope to address this before releasing to the marketplace).
  • Once a user has uploaded a file, there is no way to un-set that file except by uploading a new file to replace it. I probably won’t ever get around to implementing this because it’s too complicated given the limitations of the validation infrastructure C5 provides for these attribute types.
  • I strongly recommend against using this attribute type for pages/collections – you should stick with the built-in File Manager infrastructure for all things content-related.

Future Direction

I plan to release this for free in the marketplace eventually, but I want to add some more features first (allowing files to be sent to the File Manager, deleting old files, etc.). In the meantime, the attached package provides enough functionality for basic usage.

Comments