|
How to handle file uploadsXataface allows you to store file uploads in BLOB fields or on the file system.
Many applications need to be able to handle file uploads in some way
shape or form, whether it be for uploading a logo to accompany a
company profile or a PDF file as a resume for a job applicant.
Xataface supports file uploads in 2 flavours:
Method 1: Storing files the database (as a BLOB field)I will describe by way of example. Suppose we wish to add a field called PDFDescription to the "Course" table in our FacultyOfWidgetry application (from the Getting Started with Xataface tutorial. We want this field to store a PDF version of the course description. We do the following:
Accessing files stored in the databaseOne thing that may scare you about storing files in the database is
that they may seem less accessible than if they were on the file
system. In fact, Xataface makes it easy to access the
files. One way is to click on the "View Field Content in new
Window" link on the edit form for the record (as shown above). If
you look at the url of this link, you will notice that you can access
files directly from the URL. An example URL is: http://powerbook.local/~shannah/FacultyOfWidgetry/index.php?-action=getBlob&-table=Course&-field=PDFOutline&CourseID=1 This URL will retrieve the contents of the PDFOutline field for the course with CourseID = 1. Method 2: Storing files on the file systemSometimes it may be more convenient to store the files in a folder on the file system and just store the names of the files in the database. This is also possible with Xataface. Follow these steps to implement the previous example with file system storage:
Specifying a custom upload directoryIf, for some reason, you don't want the files to be uploaded to the tables/<tablename>/<fieldname> directory, you can specify a different directory by adding the "savepath" and "url" attributes to the fields.ini file:[PDFOutline]Note: make sure that the directory specified by "savepath" is writable by the web server. The current release of Xataface does not fail very gracefully if you forget to do this. You will just get a blank screen when you try to upload files if this directory is not writable. Future versions will provide a more descriptive error message, but for now, treat this as a warning. Restricting mimetypes and extensionsFor security reasons, it is a good idea to restrict the mimetypes and extensions that can be uploaded if you are storing files on the file system. For example: someone could upload a malicious PHP script and then access the script using their web browser causing the script to execute using the web server's permissions. For this reason it is a good idea to declare explicitly what mimetypes and file extensions are allowed to be uploaded into a particular field. This can be done using the following configuration directives in the fields.ini file:
[File]Example 2: File field allowing pdf files based on extension. [File]Example 3: File field allowing files with pdf and ppt extensions (powerpoint and pdf) [File] |