Attachment

class Nos\Attachment

Binds a file to an object.

Configuration

property Nos\Attachment::$attached

Required. The attached ID.

property Nos\Attachment::$dir

Required. Directory path, where the attachment is stored. Relative to local/data/files.

property Nos\Attachment::$alias

An URL alias to access the directory path.

property Nos\Attachment::$extensions

Array of valid files extensions.

property Nos\Attachment::$image

If true, accepts only files with an image extension. Same that extensions set to array('jpg', 'gif', 'png', 'jpeg').

property Nos\Attachment::$check

Used it to make the attachment private. A callback function to check permissions against. It takes a single parameter: Attachement instance.

Methods

static Nos\Attachment::forge($attached, $config)
Parameters:
Returns:

A new Nos\Attachment.

Nos\Attachment::newFile()
Returns:Get the new attachment file path if one, false if no file exists.
Nos\Attachment::path()
Returns:Get the attachment file path or false if no file exists.
Nos\Attachment::filename()
Returns:Get the attachment filename or false if no file exists.
Nos\Attachment::extension()
Returns:Get the attachment extension or false if no file exists.
Nos\Attachment::isImage()
Returns:True if the Attachment is an image, false otherwise.
Nos\Attachment::url($absolute = true)
Parameters:
  • $absolute (bool) – Default true, if false return relative URL
Returns:

Get the attachment url or false if no file exists.

Nos\Attachment::urlResized($max_width = 0, $max_height = 0, $absolute = true)
Parameters:
  • $max_width (array) – Max width of the image.
  • $max_height (array) – Max height of the image.
  • $absolute (bool) – Default true, if false return relative URL
Returns:

Get the resized url for the Attachment or false if no file exists or it’s not an image.

Nos\Attachment::htmlAnchor($attributes = array())
Parameters:
  • $attributes (array) –
    Array of attributes to be applied to the anchor tag.
    If key ‘text’ is set in $attributes parameter, its value replace attachment filename
Returns:

Returns an HTML anchor tag with, by default, attachment URL in href and attachment filename in text.

Nos\Attachment::getToolkitImage()
Returns:Returns a Nos\Toolkit_Image based on the attachment if is an image, False otherwise.
Nos\Attachment::set($file, $filename = null)
Parameters:
  • $file (array) – File path
  • $filename (array) – File name
Returns:

Set a new Attachment file.

Throws:

FuelCoreFileAccessException if new file have a not allowed extension.

Nos\Attachment::save()

Save a new Attachment file

Returns:True if the Attachment have been saved, false otherwise.
Nos\Attachment::delete()

Delete the Attachment file

Returns:True if the Attachment have been deleted, false otherwise.

Example

<?php

$attachment = \Nos\Attachment::forge('my_id', array(
        'dir' => 'apps'.DS.'myapps',
        'alias' => 'myapps-attachment',
        'extensions' => array('pdf'),
        'check' => 'check_attachment',
));

// It's for example
$_SESSION['user_connected'] = true;

function check_attachment($attachment) {
        return $GLOBALS['user_connected'];
}

try {
        $attachment->set('/path/a_doc.doc');
} catch (\Fuel\Core\FileAccessException $e) {
        // Exception will be throw, extension is doc, not a pdf.
}

$attachment->set('/path/a_pdf.pdf');
$attachment->save();

// Now file saved in local/data/files/apps/myapps/my_id/a_pdf.pdf

echo $attachment->url();
// Echo http://wwww.domain.ext/data/files/myapps-attachment/my_id/a_pdf.pdf

$_SESSION['user_connected'] = false;
// Now URL http://wwww.domain.ext/data/files/myapps-attachment/my_id/a_pdf.pdf return 404

$attachment->delete();