|
| genToken ($iFileId) |
|
| changeStorageEngine ($sEngine) |
|
| isAvailable () |
|
| isInstalled () |
|
| getObject () |
|
| getObjectData () |
|
| getErrorCode () |
|
| getErrorString () |
|
| getMaxUploadFileSize ($iProfileId) |
|
| storeFile ($sMethod, $aMethodParams, $sName=false, $isPrivate=true, $iProfileId=0, $iContentId=0) |
|
| convertMultipleFilesArray ($aFiles) |
|
| storeFileFromForm ($aFile, $isPrivate=true, $iProfileId=0, $iContentId=0) |
|
| storeFileFromXhr ($sName, $isPrivate=true, $iProfileId=0, $iContentId=0) |
|
| storeFileFromPath ($sPath, $isPrivate=true, $iProfileId=0, $iContentId=0) |
|
| storeFileFromUrl ($sUrl, $isPrivate=true, $iProfileId=0, $iContentId=0) |
|
| storeFileFromStorage ($aParams, $isPrivate=true, $iProfileId=0, $iContentId=0) |
|
| deleteFile ($iFileId, $iProfileId=0) |
|
| queueFilesForDeletion ($mixedFileId) |
|
| queueFilesForDeletionFromGhosts ($iProfileId, $iContentId=false) |
|
| queueFilesForDeletionFromObject () |
|
| download ($aFile, $sToken=false, $bForceDownloadDialog='auto') |
|
| setFilePrivate ($iFileId, $isPrivate=true) |
|
| getFileUrlByRemoteId ($sRemoteId) |
|
| getFileUrlById ($iFileId) |
|
| getFile ($iFileId) |
|
| getGhost ($iFileId) |
|
| isFilePrivate ($iFileId) |
|
| afterUploadCleanup ($mixedFileIds, $iProfileId, $iContentId=false) |
|
| getGhosts ($iProfileId, $iContentId=false, $isCheckAllAccountProfiles=false, $isAdmin=false) |
|
| reorderGhosts ($iProfileId, $iContentId, $aGhosts) |
|
| updateGhostsContentId ($mixedFileIds, $iProfileId, $iContentId, $isAdmin=false) |
|
| getFiles ($iProfileId) |
|
| getFilesAll ($iStart=0, $iPerPage=1000) |
|
| getRestrictionsTextExtensions ($iProfileId) |
|
| getAllowedExtensions () |
|
| getRestrictionsTextFileSize ($iProfileId) |
|
| getRestrictionsTextArray ($iProfileId) |
|
| reloadMimeTypesFromFile ($sFile) |
|
| getFileExt ($sFileName) |
|
| getFileTitle ($sFileName) |
|
| getMimeTypeByFileName ($sFileName) |
|
| getIconNameByFileName ($sFileName) |
|
| getFontIconNameByFileName ($sFileName) |
|
| insertGhost ($iFileId, $iProfileId, $iContentId=0) |
|
| onBeforeFileDelete ($aFileInfo, $iProfileId) |
|
| onFileDeleted ($aFileInfo, $iProfileId, $aGhost=false) |
|
| queueFiles ($aFiles) |
|
This class unify storage. As the result there are many advantages:
- files can be stored as on localhost as on remote storage, for example Amazon s3
- all files are in one place and separated from other files, so the data can be organised more easily, for example moved to dedicated disk if there is not enough storage
- simplicity of usage, there are hight level classes to handle all necessary operations, including upload and security
- quotas settings, so you always control how much space you are going to use
- persistent storage; uploaded, but not saved files appear upon page reload or future submission of the same form
Usage.
Step 1: Add record to 'sys_objects_storage' table, like you doing this for Comments or Voting objects:
- object - your storage object name, usually it is in the following format - vendor prefix, underscore, module prefix; for example for BoonEx Forum module it can be bx_forum.
- engine - storage engine, for now the following engines are supported:
- Local - local storage, by default files are stored in /storage/ subfolder
- S3 - Amazon S3 storage, files are stored on Amazon S3 storage, you need to point AWS Access Key, AWS Secret Key and AWS Bucket in the settings
- params - custom storage engine params as php serialized string, supported params:
- fields - list of additional fields to add to database as key(field name) and value(func or serialized service call to get the value)
- token_life - life of the security token in seconds for private files
- cache_control - control browser cache, allow browser to store files in browser's cache for this number of seconds, to disable browser cache, or let browser to decide on its own set it to 0(zero)
- levels - store files in subfolders, generated from filename; it is useful when there is limit of number of files/folders per directory; for example if level is 2 and file name is abc.jpg then the file will be stored in a/b/abc.jpg folder, set to to 0(zero) to disable this feature
- table_files - table where file info is stored, please refer to step 2 for more details
- ext_mode - file extensions restriction mode:
- allow-deny - allow only file types in ext_allow field and deny all other file types, ext_deny field is ignored.
- deny-allow - allow all files except the ones specified in ext_deny field, ext_allow field is ignored.
- ext_allow - allowed file extensions, comma separated, it is in effect when ext_mode is allow-deny; example - jpg,gif,png
- ext_deny - denied file extensions, comma separated, it is in effect when ext_mode is deny-allow; example - exe,com,bat
- quota_size - storage engine quota in bytes, the summary of all uploaded files can not be bigger than this number
- current_size - current storage engine usage, the sum of all uploaded file sizes
- quota_number - max number of files allowed in this storage engine
- current_number - current number of files in this storage engine
- max_file_size - max file size for this storage engine, please note that other server settings are used if they are less than this setting option
- ts - unix timestamp of the last file upload
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 sample store_file.php file:
require_once('./inc/header.inc.php');
require_once(BX_DIRECTORY_PATH_INC . "design.inc.php");
if (isset($_POST['add'])) {
$iId = $oStorage->storeFileFromForm($_FILES['file'], true, 0);
if ($iId) {
$iCount = $oStorage->afterUploadCleanup($iId, $iProfileId);
echo "uploaded file id: " . $iId . "(deleted orphans:" . $iCount . ")";
} else {
echo "error uploading file: " . $oStorage->getErrorString()
}
}
static pruning()
Definition BxDolStorage.php:250
static getObjectInstance($sObject)
Definition BxDolStorage.php:216
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 . "design.inc.php");
$iId = 1234;
echo "Uploaded image: <img src="' . $oStorage->getFileUrlById($iId) . '" />;";
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.