Years ago, I used to put uploaded files any random place in my sites. This turned out to be a pain when I wanted to deploy a new version of a site. If I was using FTP, I couldn't just upload a set of root folders because some folders used for uploads might be within them and I wouldn't want to overwrite files that had been uploaded on the live site. If I was using subversion, then I would have to select "ignore" on each upload location individually. Even comparisons in BeyondCompare were a little more tedious.
Basically, the problem is that uploaded files are really data. It just doesn't make sense to store that data directly in the database for performance reasons. Even so, I don't want to treat them the same as code files that I can safely deploy from the staging/development site.
FileMgr was created to solve these challenges.
The init() method of FileMgr takes one argument: UploadPath. This holds the full server path for where to store file uploads.
If I want a component to ensure the existence of a folder, I call the makeFolder() method.
Example code to initialize FileMgr:
So, if I have a CMS and I want it to put images in a "cms-images" folder (C:\inetpub\wwwroot\files\cms-images\ using the above example), I might use the following code:
<cfargument name="FileMgr" type="any" required="yes">
<cfset variables.FileMgr = arguments.FileMgr>
Uploading a file to the folder then uses the uploadFile() method with the following arguments:
- FieldName (required): The name of the form field holding the uploaded file
- Folder (optional): The folder in which to put the file
- NameConflict (optional): The value of the NameConflict attribute in cffile
The method returns a CFFILE structure.
So, uploading a file might use the following code:
Reading or deleting a file uses the readFile() and deleteFile() method respectively. Both have the same arguments:
- FileName (required): The name of the file
- Folder (optional): The folder in which the file is located
Code to delete a file might look like this:
All of this allows the component itself to not know anything about where the files are stored, other than the name of the folder being used by that component.
FileMgr is open source and free for any use.