Add attachment


The class Attachment allows you to manage attachments.

Attachments are saved in the local/data/files/ directory.

Usually attachments are joined to a Model but it is possible to manage them as you wish. You just need a tiny configuration in order to define an Attachment.


$attachment = \Nos\Attachment::forge('my_id', array(
        'dir' => 'apps'.DS.'myapps',

On above example, attachment will be saved in the local/data/files/apps/myapps/my_id/ directory.

In order to save a file, you will just need:

$attachment->set($_FILES['file']['tmp_name'],  $_FILES['file']['name']);

On above example, we save an uploaded file as an attachment. File location will be: local/data/files/apps/myapps/my_id/original_name.ext where original_name.ext is the original name of the uploaded file collected from $_FILES['file']['name'].

Joined to a model

In the case a file is joined to a Model, it is even simpler. In your class, you just need to write:

class Model_Example extends \Nos\Orm\Model

        protected static $_attachment = array(
                'avatar' => array(),
                'document' => array(),

This way, each Model_Example item will have two attachment: avatar and document.

$item = Model_Example::find('first');
$item->avatar->set($_FILES['file']['tmp_name'], $_FILES['file']['name']);


For more details, check Attachment.


When you create a new Attachment, you can specify a list of authorized extensions by adding the extensions key to the configuration. Value should be an array of authorized extensions.

If your file has to be an image, you can set the special key image to true.

URL alias

By default, your attachment will be available in this URL:

If dir value is, as often, apps/my_app/my_file_type/, the url can be quite extensive.

Define an alias class in your Attachment configuration. alias value will replace dir value in URL.

Secured attachment

It is possible to secure your attachments, in order to limit access only for authenticated user for example. You just need to define on configuration the check key which value is a fonction de callback. Each time file is requested, the system will execute this function, with first parameter the current Attachment instance, in order to check the file is available in this context.


class Verification
        public static function check($attachment)
                return isset($_SESSION['user_connected']) && $_SESSION['user_connected'];

$attachment = \Nos\Attachment::forge('my_id', array(
        'dir' => 'apps'.DS.'myapps',
        'check' => array('Verification', 'check'),

In the upper example, if the user is logged (session key user_connected set to true), the file will be available. If not, the url will throw a 404 error.

Read the Docs v: dubrovka
On Read the Docs
Project Home

Free document hosting provided by Read the Docs.