class PostsController extends AppController {
/* the rest of your controller here */
public function add() {
if ($this->request->is('post')) {
try {
$this->Post->createWithAttachments($this->request->data);
$this->Session->setFlash(__('The message has been saved'));
} catch (Exception $e) {
$this->Session->setFlash($e->getMessage());
}
}
}
}
class Post extends AppModel {
/* the rest of your model here */
public function createWithAttachments($data) {
// Sanitize your images before adding them
$images = array();
if (!empty($data['Image'][0])) {
foreach ($data['Image'] as $i => $image) {
if (is_array($data['Image'][$i])) {
// Force setting the `model` field to this model
$image['model'] = 'Post';
// Unset the foreign_key if the user tries to specify it
if (isset($image['foreign_key'])) {
unset($image['foreign_key']);
}
$images[] = $image;
}
}
}
$data['Image'] = $images;
// Try to save the data using Model::saveAll()
$this->create();
if ($this->saveAll($data)) {
return true;
}
// Throw an exception for the controller
throw new Exception(__("This post could not be saved. Please try again"));
}
}
public $validate = array(
'photo' => array(
'rule' => 'isUnderFormSizeLimit',
'message' => 'File exceeds form upload filesize limit'
)
);
public $validate = array(
'photo' => array(
'rule' => 'isCompletedUpload',
'message' => 'File was not successfully uploaded'
)
);
public $validate = array(
'photo' => array(
'rule' => 'isFileUpload',
'message' => 'File was missing from submission'
)
);
public $validate = array(
'photo' => array(
'rule' => 'isFileUploadOrHasExistingValue',
'message' => 'File was missing from submission'
)
);
public $validate = array(
'photo' => array(
'rule' => 'tempDirExists',
'message' => 'The system temporary directory is missing'
)
);
public $validate = array(
'photo' => array(
'rule' => array('tempDirExists', false),
'message' => 'The system temporary directory is missing'
)
);
public $validate = array(
'photo' => array(
'rule' => 'isSuccessfulWrite',
'message' => 'File was unsuccessfully written to the server'
)
);
public $validate = array(
'photo' => array(
'rule' => array('isSuccessfulWrite', false),
'message' => 'File was unsuccessfully written to the server'
)
);
public $validate = array(
'photo' => array(
'rule' => 'noPhpExtensionErrors',
'message' => 'File was not uploaded because of a faulty PHP extension'
)
);
public $validate = array(
'photo' => array(
'rule' => array('noPhpExtensionErrors', false),
'message' => 'File was not uploaded because of a faulty PHP extension'
)
);
public $validate = array(
'photo' => array(
'rule' => array('isValidMimeType', array('application/pdf', 'image/png')),
'message' => 'File is not a pdf or png'
)
);
public $validate = array(
'photo' => array(
'rule' => array('isValidMimeType', array('application/pdf', 'image/png'), false),
'message' => 'File is not a pdf or png'
)
);
public $validate = array(
'photo' => array(
'rule' => array('isWritable'),
'message' => 'File upload directory was not writable'
)
);
public $validate = array(
'photo' => array(
'rule' => array('isWritable', false),
'message' => 'File upload directory was not writable'
)
);
public $validate = array(
'photo' => array(
'rule' => array('isValidDir'),
'message' => 'File upload directory does not exist'
)
);
public $validate = array(
'photo' => array(
'rule' => array('isValidDir', false),
'message' => 'File upload directory does not exist'
)
);
public $validate = array(
'photo' => array(
'rule' => array('isBelowMaxSize', 1024),
'message' => 'File is larger than the maximum filesize'
)
);
public $validate = array(
'photo' => array(
'rule' => array('isBelowMaxSize', 1024, false),
'message' => 'File is larger than the maximum filesize'
)
);
public $validate = array(
'photo' => array(
'rule' => array('isAboveMinSize', 1024),
'message' => 'File is below the mimimum filesize'
)
);
public $validate = array(
'photo' => array(
'rule' => array('isAboveMinSize', 1024, false),
'message' => 'File is below the mimimum filesize'
)
);
public $validate = array(
'photo' => array(
'rule' => array('isValidExtension', array('pdf', 'png', 'txt')),
'message' => 'File does not have a pdf, png, or txt extension'
)
);
public $validate = array(
'photo' => array(
'rule' => array('isValidExtension', array('pdf', 'png', 'txt'), false),
'message' => 'File does not have a pdf, png, or txt extension'
)
);
public $validate = array(
'photo' => array(
'rule' => array('isAboveMinHeight', 150),
'message' => 'File is below the minimum height'
)
);
public $validate = array(
'photo' => array(
'rule' => array('isAboveMinHeight', 150, false),
'message' => 'File is below the minimum height'
)
);
public $validate = array(
'photo' => array(
'rule' => array('isBelowMaxHeight', 150),
'message' => 'File is above the maximum height'
)
);
public $validate = array(
'photo' => array(
'rule' => array('isBelowMaxHeight', 150, false),
'message' => 'File is above the maximum height'
)
);
public $validate = array(
'photo' => array(
'rule' => array('isAboveMinWidth', 150),
'message' => 'File is below the minimum width'
)
);
public $validate = array(
'photo' => array(
'rule' => array('isAboveMinWidth', 150, false),
'message' => 'File is below the minimum width'
)
);
public $validate = array(
'photo' => array(
'rule' => array('isBelowMaxWidth', 150),
'message' => 'File is above the maximum width'
)
);
public $validate = array(
'photo' => array(
'rule' => array('isBelowMaxWidth', 150, false),
'message' => 'File is above the maximum width'
)
);