The aim of introducing storage objects in Dolphin 8 is to unify files storage. As the result there are many advantages:
Step 1: Add record to sys_objects_storage table, like you doing this for Comments or Voting objects.
Step 2: Create table for files.
CREATE TABLE `my_sample_files` ( `id` int(11) NOT NULL AUTO_INCREMENT, `profile_id` int(10) unsigned NOT NULL, `remote_id` varchar(255) NOT NULL, `path` varchar(255) NOT NULL, `file_name` varchar(255) NOT NULL, `mime_type` varchar(128) NOT NULL, `ext` varchar(32) NOT NULL, `size` int(11) NOT NULL, `added` int(11) NOT NULL, `modified` int(11) NOT NULL, `private` int(11) NOT NULL, PRIMARY KEY (`id`), UNIQUE KEY `remote_id` (`remote_id`) );
You need to enter this table name in table_files field in sys_objects_storage table, mentioned in step 1.
The files will be added to this table automatically, all you need is to save id from this table, so you can refer to the file by the id.
It is not recommended to change this table, it is better to create another table which will be connected with this one by file id.
Step 3: Handling upload.
Sample HTML form:
<form enctype="multipart/form-data" method="POST" action="store_file.php"> Choose a file to upload: <input name="file" type="file" /> <br /> <input type="submit" name="add" value="Upload File" /> </form>
Add server code in store_file.php:
require_once('./inc/header.inc.php'); require_once(BX_DIRECTORY_PATH_INC . "languages.inc.php"); require_once(BX_DIRECTORY_PATH_INC . "params.inc.php"); require_once(BX_DIRECTORY_PATH_INC . "design.inc.php"); bx_import('BxDolStorage'); $oStorage = BxDolStorage::getObjectInstance('my_module'); // create storage object instance, 'my_module' is value of 'object' field in 'sys_objects_storage' table if (isset($_POST['add'])) { // if form is submitted $iId = $oStorage->storeFileFromForm($_FILES['file'], true, 0); // store file from submitted HTML form, 'file' is input name with field, true means store file as private, 0 is profile id if ($iId) { // storeFileFromForm returns file id, not false value means operation is successful. // save $iId somewhere, so you can refer to the file after $iCount = $oStorage->afterUploadCleanup($iId, $iProfileId); // since we saved $iId, we remove it from the orphans list, so it will not appear on the form next time (persistent storage) echo "uploaded file id: " . $iId . "(deleted orphans:" . $iCount . ")"; } else { // something went wrong - print error code echo "error uploading file: " . $oStorage->getErrorCode() } }
Please refer to the functions definition for more additional description of functions params.
Step 4: Displaying the file.
Use the following code to retrieve saved file. Remember you saved filed id somewhere in the previous step.
Lets assume that the uploaded file is image, then we can show it using the following code:
require_once('./inc/header.inc.php'); require_once(BX_DIRECTORY_PATH_INC . "languages.inc.php"); require_once(BX_DIRECTORY_PATH_INC . "params.inc.php"); require_once(BX_DIRECTORY_PATH_INC . "design.inc.php"); bx_import('BxDolStorage'); $oStorage = BxDolStorage::getObjectInstance('my_module'); $iId = 1234; // since you've saved it somewhere in the previous step, you can retrieve it here echo "Uploaded image: <img src="' . $oStorage->getFileUrlById($iId) . '" />l;";
It will show the file, regardless if it is private or public. You need to control it by yourself who will view the file. The difference in viewing private files is that link to the file is expiring after N seconds, you control this period using token_life field in sys_objects_storage table.
Using simple high level functions you can implement files storage with all the error checking, security and flexibility. Try to change storage engine from Local to S3 in sys_objects_storage table and files will be stored directly on Amazon S3, no other changes are required in your code.
The process can be even more automated using forms and new uploaders!
Changelog:
2011-10-17: typo in code comments
2011-11-09: "pruning" is removed from sample code since it is already called automatically in Dolphin system