pclib  3.3.0
Lightweight PHP framework
FileStorage Class Reference

Provides file management functions. More...

Inheritance diagram for FileStorage:
BaseObject

Public Member Functions

 __construct ($rootDir='')
 
 getFile ($loc, $withContent=false)
 Return file from location $loc. More...
 
 setFile ($loc, $data)
 Store file $data into location $loc (insert or update). More...
 
 copyFile ($path, $loc)
 Copy file from directory path $path into filestorage location $loc. More...
 
 addFile ($loc, $data)
 Add file $data into location $loc. More...
 
 deleteFile ($loc)
 Delete file into location $loc. More...
 
 getFiles ($loc)
 Return array of all files assigned to entity $loc. More...
 
 setFiles ($loc, $files)
 Store all $files into file storage and assign it to entity $loc. More...
 
 deleteFiles ($loc)
 Delete all files assigned to entity $loc. More...
 
 addFiles ($loc, $files)
 Add all $files into file storage and assign it to entity $loc. More...
 
 postedFiles ($input_id=null)
 Return array of files submitted by some form. More...
 
 output ($loc, $showDownload=false)
 Output file $loc (such as image or pdf) into the browser. More...
 
 isImage ($file)
 Check if file is image.
 
 fileInBlackList ($fileName)
 Test if uploaded filename mask is on the blacklist.
 
 getErrorMessage ($code)
 Return upload error message.
 
- Public Member Functions inherited from BaseObject
 loadDefaults ($className=null)
 Load default parameters of class $className into object instance.
 
 setProperties (array $defaults)
 Set public properties of object from the array. More...
 
 toArray ()
 Convert object to array. More...
 

Data Fields

 $TABLE = 'FILESTORAGE'
 Database table name.
 
 $errors = []
 If upload error occurs, this will contains error messages.
 
 $fileNameFormat = "{ENTITY_TYPE}_{HASH}.{EXT}"
 You can use fields: HASH,EXT,ORIGNAME,ORIGNAME_NORMALIZED,FILE_ID or any field from $entity.
 
 $dirNameFormat = ''
 Unused - always "/Y/n/" in this version.
 
 $uploadBlackList = ['*.php','*.php?','*.phtml','*.exe','.htaccess']
 Files matching patterns cannot be uploaded.
 
- Data Fields inherited from BaseObject
 $serviceLocator
 var function() Return service object when requested with service().
 

Protected Member Functions

 insertFile (array $loc, array $data)
 Upload new file into filesystem and create db record.
 
 normalize ($filename)
 Sanitize filename. More...
 
 getDir ($format, $file)
 Create and return directory name where file $file will be stored. More...
 
 getFileName ($format, $file)
 Return filename used for storing particular $file. More...
 
- Protected Member Functions inherited from BaseObject
 service ($service, $default=null)
 Try acquire $service and load it into property $this->$service. More...
 

Protected Attributes

 $rootDir = ''
 Path to your writable storage directory.
 

Additional Inherited Members

- Static Public Member Functions inherited from BaseObject
static defaults (... $args)
 Set or retireve default parameters of the object. More...
 

Detailed Description

Provides file management functions.

You can upload, store, list and manage files.

  • Files are stored in directory structure 'rootdir/year/month/' by default.
  • Each file has record in database table FILESTORAGE and it is assigned to some entity (specified by location $loc).
  • Examples: $fs->setFiles(['products', 1], $fs->postedFiles()); //Save posted files $fs->getFiles(['products', 1]); //return array with list of files for product_id=1 $fs->getFile(123); //return file with id=123

Constructor & Destructor Documentation

◆ __construct()

__construct (   $rootDir = '')
Parameters
$rootDirPath to your writable storage directory.

Member Function Documentation

◆ addFile()

addFile (   $loc,
  $data 
)

Add file $data into location $loc.

Parameters
int | array$locFile location
array$dataFile data
Returns
int $id of file

◆ addFiles()

addFiles (   $loc,
  $files 
)

Add all $files into file storage and assign it to entity $loc.

Parameters
int | array$loc[entity-type, entity-id]
array$filesList of files

◆ copyFile()

copyFile (   $path,
  $loc 
)

Copy file from directory path $path into filestorage location $loc.

Parameters
string$pathFull source path
int | array$locTarget location
Returns
int $id of file

◆ deleteFile()

deleteFile (   $loc)

Delete file into location $loc.

Parameters
int | array$locFile location
Returns
$file File data

◆ deleteFiles()

deleteFiles (   $loc)

Delete all files assigned to entity $loc.

Parameters
int | array$loc[entity-type, entity-id]

◆ getDir()

getDir (   $format,
  $file 
)
protected

Create and return directory name where file $file will be stored.

It is creating and using '/Year/month/' directories by default. Parameters $format and $file are unused in current version.

◆ getFile()

getFile (   $loc,
  $withContent = false 
)

Return file from location $loc.

Parameters
int | array$locFile location
bool$withContentReturn content of the file too
Returns
array|false $file File data from storage

◆ getFileName()

getFileName (   $format,
  $file 
)
protected

Return filename used for storing particular $file.

You can setup filename format by setting attribute FileStorage->fileNameFormat.

Parameters
$formatFormat string e.g. "PREFIX_{ORIGNAME_NORMALIZED}.{EXT}";
array$fileFile information (Any field can be used in $format string)

◆ getFiles()

getFiles (   $loc)

Return array of all files assigned to entity $loc.

Parameters
int | array$loc[entity-type, entity-id]
Returns
array $files List of files

◆ normalize()

normalize (   $filename)
protected

Sanitize filename.

Replace non-ascii characters, remove diacriticts and replace whitespaces with '_'.

◆ output()

output (   $loc,
  $showDownload = false 
)

Output file $loc (such as image or pdf) into the browser.

Parameters
int | array$locfile location (you can use integer id too)
bool$showDownloadShould browser show download dialog?

◆ postedFiles()

postedFiles (   $input_id = null)

Return array of files submitted by some form.

You can store them by function setFiles().

Parameters
string$input_idIf present, it will return data from this input only
Returns
array $files List of files

◆ setFile()

setFile (   $loc,
  $data 
)

Store file $data into location $loc (insert or update).

Parameters
int | array$locFile location
array$dataFile data
Returns
int $id of file

◆ setFiles()

setFiles (   $loc,
  $files 
)

Store all $files into file storage and assign it to entity $loc.

Parameters
int | array$loc[entity-type, entity-id]
array$filesList of files

The documentation for this class was generated from the following file: