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: - $attached (string) – The attached ID.
- $config (mixed) –
If is a string, use it to load configuration array. Array otherwise:
dir: Attachment::$dir alias: Attachment::$alias extensions: Attachment::$extensions image: Attachment::$image check: Attachment::$check
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()¶
Returns: Get the attachment url or false if no file exists.
- Nos\Attachment::urlResized($max_width = 0, $max_height = 0)¶
Parameters: - $max_width (array) – Max width of the image.
- $max_height (array) – Max height of the image.
Returns: Get the resized url for the Attachment or false if no file exists or it’s not an image.
- 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
- Nos\Attachment::delete()¶
Delete the Attachment file
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 data/files/myapps-attachment/my_id/a_pdf.pdf
$_SESSION['user_connected'] = false;
// Now URL data/files/myapps-attachment/my_id/a_pdf.pdf return 404
$attachment->delete();