Filestore

Filestore is a mechanism of Agile Toolkit to simplify file uploading, storage and association with your database records. Typically the choice on how files are stored by the system is done by a web developer. File data is either stored in the SQL or is kept in a file. In some cases it will be uploaded into the CND (cloud) later.

Agile Toolkit abstracts file uploading by introducing a standard Model for your files and images. This is implemented in a "filestore" add-on.

https://github.com/atk4/atk4-addons/tree/master/filestore

How files are managed?

File uploads in PHP are automatically stored in a temporary folder and then your PHP application needs to put it in a safe location. Filestore does that automatically by putting file inside upload folder. It starts by creating sub-directory '0' and placing files with random names in there. When large number of files is achieved, it will switch to folder '1' and so on until 256 subdirectories are allocated with 4000 files in each. Afterwards new files will be distributed across all the folders. A single volume can store up to 4096*256*256 files (which is a artificial limit). You can have multiple volumes and the filestore will randomly pick enabled volume.

Filenames are generated randomly and are difficult to guess, preventing someone from scanning folder for additional images.

Filestore Introduction

Filestore is an addon which enables developer to implement file upload, storage and manipulation in the unique and consistent way.

Installation Instructions

If Filestore is already implemented and you only need to install it, you should go through a number of steps.

Directory for file storage

Files are being stored in a directory which is associated with the volume. In order to be able to store files you need at least one volume and one writable directory. Create directory first, usually creating "upload" directory inside your webroot is the most convenient way. Secondly find "Filestore admin" page inside your application and add new volume. Make sure you set it as default volume.

Result

You can use absolute path too
Be sure to check this one!
 

Code

$f=$p->add('MVCForm');
$f->setModel('Filestore_Volume');
$f->set('name','vol1');
$f->set('dirname','upload');
$f->getElement('dirname')->add('Text',null,'after_field')
->
set('<ins>You can use absolute path too</ins>');
$f->getElement('enabled')->add('Text',null,'after_field')
->
set('<ins>Be sure to check this one!</ins>');
if(
$f->isSubmitted())$f->js()->univ()
    ->
alert('This is only an example form')
    ->
execute();

Files are being stored in a directory which is associated with the volume. In order to be able to store files you need at least one volume and one writable directory. Create directory first, usually creating "upload" directory inside your webroot is the most convenient way. Secondly find "Filestore admin" page inside your application and add new volume. Make sure you set it as default volume.

Configure file types

Depending on the type of your application, it might require to pre-define all types of uploaded files. If it is necessary to add support for new filetypes, add them into File Types list. Normally adding jpeg, gif and png is most common use.

Result

Id Name Mime Type Extension
1 image image/jpeg image
2 png image/png png
3 gif image/gif gif
 

Code

$f=$p->add('MVCGrid');
$f->setModel('Filestore_Type');

Test file upload

File admin has a test upload control. Once you have set up everything, you can try to upload file. It should appear in the list of uploaded files below.

Result

Previously Uploaded Files

Id Internal Name Filesize
639 0/fsTVN1t5 9049
638 0/fsD5yYtm 12131
634 0/fsl43ueC 9049
632 0/fshaMddh 5773
631 0/fsIGgVHh 677289
 

Code

$f=$p->add('Form');
$c=$p->add('Controller_Filestore_File');
$c->setActualFields(array('id','filestore_type_id','filestore_volume_id','filename','filesize'));
$f->addField('upload','upload')->setController($c);
$p->add('H4')->set('Previously Uploaded Files');
$g=$p->add('MVCGrid')->setController($c);
$g->dq->limit(5)->order('id desc');