J'ai trouvé comment limiter la taille des fichier à uploader ainsi que la limit
Dans mon cas je gère les TopicsController.
Il faud dans le modele topics ajouter ceci:
<?php
class Topic extends AppModel {
public $actsAs = array(
'Media.Media' => array(
"path"=>"/files/Images/Topics/%id/%f",
"max_width"=>1500,
"max_height"=>1500,
"limit"=>4
)
);
}
Maintenant, je ne sais pas si j'ai tout fait juste. Ca limite bien la taille des image et la quantité. Mais quand j'upload une image trop grand, il ne me génère pas de message d'erreur. J'aimerais bien avoir un message d'erreur, pourqu'au, si mon colègue utilise cet outil sache ce qu'il se passe.
J'ai vu ceci dans le plugin. Mais je n'arrive pas (du moins je ne comprends pas) comme afficher le messaghe en question.
$this->error = __d('media', "La largeur maximum autorisÈe est de %dpx", $model->medias'max_width']);
Merci pour votre aide!!
public function beforeSave($options = array()){
if( isset($this->data$this->alias]'ref'])){
$ref = $this->data'Media']'ref'];
$model = ClassRegistry::init($ref);
if(!in_array('Media', $model->Behaviors->loaded())){
throw new CakeException(__d('media',"Le model '%s' n'a pas le comportement 'Media'", $ref));
}
}
if( isset($this->data'Media']'file']) && is_array($this->data'Media']'file']) && isset($this->data'Media']'ref']) ){
$model = ClassRegistry::init($this->data'Media']'ref']);
$ref_id = $this->data'Media']'ref_id'];
$pathinfo = pathinfo($this->data'Media']'file']'name']);
$extension = strtolower($pathinfo'extension']) == 'jpeg' ? 'jpg' : strtolower($pathinfo'extension']);
if(!in_array($extension, $model->medias'extensions'])){
$this->error = __d('media','Vous ne pouvez pas uploader ce type de fichier (%s seulement)', implode(', ', $model->medias'extensions']));
return false;
}
// Limit files count by ref/ref_id
if ($model->medias'limit'] > 0 && $this->data'Media']'ref_id'] > 0) {
$qty = $this->find('count', array('conditions' => array('ref' => $this->data'Media']'ref'], 'ref_id' => $this->data'Media']'ref_id'])));
if ($qty >= $model->medias'limit']) {
$this->error = __d('media', "Vous ne pouvez envoyer qu'un nombre limitÈ de fichier (%d). Veuillez en supprimer avant d'en envoyer de nouveau.", $model->medias'limit']);
return false;
}
}
// Limit image size (for png/jpg/bmp/tiff)
if (in_array($extension, array('jpg', 'png', 'bmp', 'tiff')) && ($model->medias'max_width'] > 0 || $model->medias'max_height'] > 0 )) {
list($width,$height) = getimagesize($this->data'Media']'file']'tmp_name']);
if ($model->medias'max_width'] > 0 && $width > $model->medias'max_width']) {
$this->error = __d('media', "La largeur maximum autorisÈe est de %dpx", $model->medias'max_width']);
return false;
}
if ($model->medias'max_height'] > 0 && $height > $model->medias'max_height']) {
$this->error = __d('media', "La hauteur maximum autorisÈe est de %dpx", $model->medias'max_height']);
return false;
}
}
// Limit Image size
if ($model->medias'size'] > 0 && floor($this->data'Media']'file']'size'] / 1024) > $model->medias'size']) {
$humanSize = $model->medias'size'] > 1024 ? round($model->medias'size']/1024,1).' Mo' : $model->medias'size'].' Ko';
$this->error = __d('media', "Vous ne pouvez pas envoyer un fichier supÈrieur ‡ %s", $humanSize);
return false;
}
if(method_exists($this->data'Media']'ref'], 'uploadMediasPath')){
$path = $model->uploadMediasPath($ref_id);
}else{
$path = $model->medias'path'];
}
$filename = Inflector::slug($pathinfo'filename'],'-');
$search = array('/', '%id', '%mid', '%cid', '%y', '%m', '%f');
$replace = array(DS, $ref_id, ceil($ref_id/1000), ceil($ref_id/100), date('Y'), date('m'), Inflector::slug($filename));
$file = str_replace($search, $replace, $path) . '.' . $extension;
$this->testDuplicate($file);
if(!file_exists(dirname(WWW_ROOT.$file))){
mkdir(dirname(WWW_ROOT.$file),0777,true);
}
$this->move_uploaded_file($this->data'Media']'file']'tmp_name'], WWW_ROOT.$file);
chmod(WWW_ROOT.$file,0777);
$this->data'Media']'file'] = '/' . trim(str_replace(DS, '/', $file), '/');
}
return true;
}