Upload

This class is for managing uploads, each uploaded file and it's actions will be wrapped inside an instance of "IUploadedFile".
You can access the "IUploadedFile" object(s) or iterate over them via the "IUpload" instance. The "IUpload" instance itself is accessed and created via the "IRequest" instance. e.g: "$uploadedFile = $request->Upload->get('upload-key');" or "foreach ($request->Upload as $uploadKey => $uploadedFileObj)".
As instanced of this class are created and accessed via an "IRequest", this class is not included into the autoload.
The upload "key" argument of the methods of this class:
The key comes from the value of the "name" attribute of the input elements used in the HTML for file uploading. If the name value is array style e.g. "file[1][2]" then the key to identify the upload to a method of this class would be as "file.1.2", each array key is glued to the others by a dot.
Source
Upload.php
Implements
IUpload , IteratorAggregate
Depends on
ArrayIterator , Inspect , UploadedFile , UploadError

Static Methods

getApcProgress

array Upload::getApcProgress(string key)

Get progress inf using APC. Requires APC and php.ini setings: post_max_size = XXXM
upload_max_filesize = XXXM
apc.max_file_size = XXM
apc.rfc1867 = on

Arguments

    key (string)
    string File upload key

Returns

array
Progress status
{
    string key        : upload file key,
    int    err        : 0=no error - 1=error raised,
    bool   done       : true=upload finished - false=upload in progress,
    int    percent    : percent uploaded,
    string start_time : start date-time,
    int    total      : (bytes) total file size,
    int    current    : (bytes) bytes uploaded,
    int    rate       : (byte/second) upload rate,
    int    left_time  : (seconds) estimated time left,
}

isApcCacheProgressEnabled

boolean Upload::isApcCacheProgressEnabled()

Can your system provide progress using APC.

Returns

boolean
true | false

Methods

__construct

Upload $obj = new Upload()

Crates Upload instance. Loads data from $_FILES.

Returns

void

get

IUploadedFile|array $obj->get(string key)

Get the uploaded file object ("IUploadedFile" instance) mapped to the key or return all of the uploaded file objects ("IUploadedFile" instances) if key is not passed.

Arguments

    key (string)
    Key to an uploaded file, if there are a series of keys then use "." between the keys e.g. "file.1.2" means "file[1][2]", if nothing is passed the files will be returned as [key: UploadedFile-instance,]

Returns

IUploadedFile|array
The uploaded file object mapped to the key or the whole array or uploaded file objects

getFiles

array $obj->getFiles([string] keys)

Get info about uploaded file(s). The file structure is the same as the file input names e.g. name="file[pic][1][2]" : {file: {1: {2: UploadedFile-instance,},},}

Arguments

    keys ([string])
    Key or a series of keys to an uploaded file, e.g. passing arguments as "file", "pic", 1, 2 would return the "upload-info" if nothing is passed the whole files upload-info array will be returned

Returns

array
the array set the key or a series of keys or null=if key not exists,

has

bool $obj->has(string key)

Check if a file upload key exists.

Arguments

    key (string)
    Key to be checked

Returns

bool
true=key exists

moveTo

array $obj->moveTo(string | array destination, bool throw=false)

Batch copy all uploaded files to a destination. If destination is a directory name then the files will be moved to it with their original filename. The destination can also be an array that maps each upload key to the path (directory and filename) where it should be moved to {upload-key: path,}. It is more versatile to manually iterate over the "IUpload" instance and handle each upload separately.

Arguments

    destination (string | array)
    The destination folder or a map of upload-keys to paths
    throw=false (bool)
    true=throw an exception on the first error, false=take it easy on errors and try to move the next uploaded file

Returns

array
result {upload-key: true, upload-key: error-msg,}

uploadFromPut

void $obj->uploadFromPut(string path=null, int chunk=1024)

Upload file(s) from a put request.

Arguments

    path=null (string)
    File-path
    chunk=1024 (int)
    Chunk size

Returns

void

validate

bool|array $obj->validate(array rules)

Validate all uploaded files. Will iterate on all uploaded files and validate them using the rules. Returns "true" if all uploaded files are validated or an array if the errors mapped to each upload key.

Arguments

    rules (array)
    Validating rules
    {
        bool          required : true
            if file has not been uploaded an "UploadError"
            will be thrown,
        int|string    size     :
            int=number of bytes string=number+unit e.g. "12M", "100B", "2K"
            if the uploaded file-size is greater than this number then an
            "UploadError" will be thrown,
        string|array  type     :
            if the mime-type of the uploaded file is not equal to this
            value(if string) or not exists in the list(if array) then an
            "UploadError" will be thrown,
        string|array  ext      :
            if the file-name extension of the uploaded file is not equal to
            this value(if string) or not exists in the list(if array) then an
            "UploadError" will be thrown,
        array|string  plugin   :
            function or method to validate the upload - the UploadFile instance
            is passed to the function/method and if the function/method returns
            false then an "UploadError" will be thrown.
            The function/method can be as
            "functionName"
            "ClassName::methodName"
            ["ClassName", "methodName"]
            [object, "methodName"]
        string        check   :
            alias for "plugin",
    }
    

Returns

bool|array
true=upload is validated, array=errors {upload-key: {error-code :msg},}, use "===" for checking this value e.g. "if (($errors=$file->validate(rules)) === true) {ok;} else {bad;}"