CakePHP-Fr.Org

Forum francophone du Framework CakePHP

Vous n'êtes pas identifié.

#1 09-03-2016 10:28:04

koukou
Crêpe Suzette
Date d'inscription: 15-11-2015
Messages: 8

problème avec le plugin uploader

Bonjour,

j'essaye d'utiliser le plugin uploader dans mon application, l'image se charge mais j'ai erreur suivante:
Array to string conversion [CORE\Cake\Model\Datasource\DboSource.php, line 1007]

[CakeException] Headers already sent in C:\wamp\www\Development\lib\Cake\Utility\Debugger.php on line 801 #0 C:\wamp\www\Development\lib\Cake\Network\CakeResponse.php(419): CakeResponse->_sendHeader('HTTP/1.1 500 In...') #1 C:\wamp\www\Development\lib\Cake\Error\ExceptionRenderer.php(327): CakeResponse->send() #2 C:\wamp\www\Development\lib\Cake\Error\ExceptionRenderer.php(306): ExceptionRenderer->_outputMessageSafe('error500') #3 C:\wamp\www\Development\lib\Cake\Error\ExceptionRenderer.php(210): ExceptionRenderer->_outputMessage('fatalError') #4 [internal function]: ExceptionRenderer->_cakeError(Object(FatalErrorException)) #5 C:\wamp\www\Development\lib\Cake\Error\ExceptionRenderer.php(187): call_user_func_array(Array, Array) #6 C:\wamp\www\Development\lib\Cake\Error\ErrorHandler.php(119): ExceptionRenderer->render() #7 [internal function]: ErrorHandler::handleException(Object(FatalErrorException)) #8 C:\wamp\www\Development\lib\Cake\Error\ErrorHandler.php(252): call_user_func('ErrorHandler::h...', Object(FatalErrorException)) #9 C:\wamp\www\Development\lib\Cake\Error\ErrorHandler.php(203): ErrorHandler::handleFatalError(256, '[CakeException]...', 'C:\\wamp\\www\\Dev...', 128) #10 [internal function]: ErrorHandler::handleError(256, '[CakeException]...', 'C:\\wamp\\www\\Dev...', 128, Array) #11 C:\wamp\www\Development\lib\Cake\Error\ErrorHandler.php(128): trigger_error('[CakeException]...', 256) #12 [internal function]: ErrorHandler::handleException(Object(FatalErrorException)) #13 C:\wamp\www\Development\lib\Cake\Error\ErrorHandler.php(252): call_user_func('ErrorHandler::h...', Object(FatalErrorException)) #14 C:\wamp\www\Development\lib\Cake\Error\ErrorHandler.php(203): ErrorHandler::handleFatalError(256, '[CakeException]...', 'C:\\wamp\\www\\Dev...', 128) #15 [internal function]: ErrorHandler::handleError(256, '[CakeException]...', 'C:\\wamp\\www\\Dev...', 128, Array) #16 C:\wamp\www\Development\lib\Cake\Error\ErrorHandler.php(128): trigger_error('[CakeException]...', 256) #17 [internal function]: ErrorHandler::handleException(Object(FatalErrorException)) #18 C:\wamp\www\Development\lib\Cake\Error\ErrorHandler.php(252): call_user_func('ErrorHandler::h...', Object(FatalErrorException)) #19 C:\wamp\www\Development\lib\Cake\Error\ErrorHandler.php(203): ErrorHandler::handleFatalError(256, '[CakeException]...', 'C:\\wamp\\www\\Dev...', 128) #20 [internal function]: ErrorHandler::handleError(256, '[CakeException]...', 'C:\\wamp\\www\\Dev...', 128, Array) #21 C:\wamp\www\Development\lib\Cake\Error\ErrorHandler.php(128): trigger_error('[CakeException]...', 256) #22 [internal function]: ErrorHandler::handleException(Object(FatalErrorException)) #23 C:\wamp\www\Development\lib\Cake\Error\ErrorHandler.php(252): call_user_func('ErrorHandler::h...', Object(FatalErrorException)) #24 C:\wamp\www\Development\lib\Cake\Error\ErrorHandler.php(203): ErrorHandler::handleFatalError(256, '[CakeException]...', 'C:\\wamp\\www\\Dev...', 128) #25 [internal function]: ErrorHandler::handleError(256, '[CakeException]...', 'C:\\wamp\\www\\Dev...', 128, Array) #26 C:\wamp\www\Development\lib\Cake\Error\ErrorHandler.php(128): trigger_error('[CakeException]...', 256) #27 [internal function]: ErrorHandler::handleException(Object(FatalErrorException)) #28 C:\wamp\www\Development\lib\Cake\Error\ErrorHandler.php(252): call_user_func('ErrorHandler::h...', Object(FatalErrorException)) #29 C:\wamp\www\Development\lib\Cake\Error\ErrorHandler.php(203): ErrorHandler::handleFatalError(256, '[CakeException]...', 'C:\\wamp\\www\\Dev...', 128) #30 [internal function]: ErrorHandler::handleError(256, '[CakeException]...', 'C:\\wamp\\www\\Dev...', 128, Array) #31 C:\wamp\www\Development\lib\Cake\Error\ErrorHandler.php(128): trigger_error('[CakeException]...', 256) #32 [internal function]: ErrorHandler::handleException(Object(FatalErrorException)) #33 C:\wamp\www\Development\lib\Cake\Error\ErrorHandler.php(252): call_user_func('ErrorHandler::h...', Object(FatalErrorException)) #34 C:\wamp\www\Development\lib\Cake\Error\ErrorHandler.php(203): ErrorHandler::handleFatalError(256, '[CakeException]...', 'C:\\wamp\\www\\Dev...', 128) #35 [internal function]: ErrorHandler::handleError(256, '[CakeException]...', 'C:\\wamp\\www\\Dev...', 128, Array) #36 C:\wamp\www\Development\lib\Cake\Error\ErrorHandler.php(128): trigger_error('[CakeException]...', 256) #37 [internal function]: ErrorHandler::handleException(Object(FatalErrorException)) #38 C:\wamp\www\Development\lib\Cake\Error\ErrorHandler.php(252): call_user_func('ErrorHandler::h...', Object(FatalErrorException)) #39 C:\wamp\www\Development\lib\Cake\Error\ErrorHandler.php(203): ErrorHandler::handleFatalError(256, '[CakeException]...', 'C:\\wamp\\www\\Dev...', 128) #40 [internal function]: ErrorHandler::handleError(256, '[CakeException]...', 'C:\\wamp\\www\\Dev...', 128, Array) #41 C:\wamp\www\Development\lib\Cake\Error\ErrorHandler.php(128): trigger_error('[CakeException]...', 256) #42 [internal function]: ErrorHandler::handleException(Object(FatalErrorException)) #43 C:\wamp\www\Development\lib\Cake\Error\ErrorHandler.php(252): call_user_func('ErrorHandler::h...', Object(FatalErrorException)) #44 C:\wamp\www\Development\lib\Cake\Error\ErrorHandler.php(203): ErrorHandler::handleFatalError(256, '[CakeException]...', 'C:\\wamp\\www\\Dev...', 128) #45 [internal function]: ErrorHandler::handleError(256, '[CakeException]...', 'C:\\wamp\\www\\Dev...', 128, Array) #46 C:\wamp\www\Development\lib\Cake\Error\ErrorHandler.php(128): trigger_error('[CakeException]...', 256) #47 [internal function]: ErrorHandler::handleException(Object(FatalErrorException)) #48 C:\wamp\www\Development\lib\Cake\Error\ErrorHandler.php(252): call_user_func('ErrorHandler::h...', Object(FatalErrorException)) #49 C:\wamp\www\Development\lib\Cake\Error\ErrorHandler.php(203): ErrorHandler::handleFatalError(256, '[CakeException]...', 'C:\\wamp\\www\\Dev...', 128) #50 [internal function]: ErrorHandler::handleError(256, '[CakeException]...', 'C:\\wamp\\www\\Dev...', 128, Array) #51 C:\wamp\www\Development\lib\Cake\Error\ErrorHandler.php(128): trigger_error('[CakeException]...', 256) #52 [internal function]: ErrorHandler::handleException(Object(FatalErrorException)) #53 C:\wamp\www\Development\lib\Cake\Error\ErrorHandler.php(252): call_user_func('ErrorHandler::h...', Object(FatalErrorException)) #54 C:\wamp\www\Development\lib\Cake\Error\ErrorHandler.php(203): ErrorHandler::handleFatalError(256, '[CakeException]...', 'C:\\wamp\\www\\Dev...', 128) #55 [internal function]: ErrorHandler::handleError(256, '[CakeException]...', 'C:\\wamp\\www\\Dev...', 128, Array) #56 C:\wamp\www\Development\lib\Cake\Error\ErrorHandler.php(128): trigger_error('[CakeException]...', 256) #57 [internal function]: ErrorHandler::handleException(Object(FatalErrorException)) #58 C:\wamp\www\Development\lib\Cake\Error\ErrorHandler.php(252): call_user_func('ErrorHandler::h...', Object(FatalErrorException)) #59 C:\wamp\www\Development\lib\Cake\Error\ErrorHandler.php(203): ErrorHandler::handleFatalError(256, '[CakeException]...', 'C:\\wamp\\www\\Dev...', 128) #60 [internal function]: ErrorHandler::handleError(256, '[CakeException]...', 'C:\\wamp\\www\\Dev...', 128, Array) #61 C:\wamp\www\Development\lib\Cake\Error\ErrorHandler.php(128): trigger_error('[CakeException]...', 256) #62 [internal function]: ErrorHandler::handleException(Object(FatalErrorException)) #63 C:\wamp\www\Development\lib\Cake\Error\ErrorHandler.php(252): call_user_func('ErrorHandler::h...', Object(FatalErrorException)) #64 C:\wamp\www\Development\lib\Cake\Error\ErrorHandler.php(203): ErrorHandler::handleFatalError(256, '[CakeException]...', 'C:\\wamp\\www\\Dev...', 128) #65 [internal function]: ErrorHandler::handleError(256, '[CakeException]...', 'C:\\wamp\\www\\Dev...', 128, Array) #66 C:\wamp\www\Development\lib\Cake\Error\ErrorHandler.php(128): trigger_error('[CakeException]...', 256) #67 [internal function]: ErrorHandler::handleException(Object(FatalErrorException)) #68 C:\wamp\www\Development\lib\Cake\Error\ErrorHandler.php(252): call_user_func('ErrorHandler::h...', Object(FatalErrorException)) #69 C:\wamp\www\Development\lib\Cake\Error\ErrorHandler.php(203): ErrorHandler::handleFatalError(256, '[CakeException]...', 'C:\\wamp\\www\\Dev...', 128) #70 [internal function]: ErrorHandler::handleError(256, '[CakeException]...', 'C:\\wamp\\www\\Dev...', 128, Array) #71 C:\wamp\www\Development\lib\Cake\Error\ErrorHandler.php(128): trigger_error('[CakeException]...', 256) #72 [internal function]: ErrorHandler::handleException(Object(FatalErrorException)) #73 C:\wamp\www\Development\lib\Cake\Error\ErrorHandler.php(252): call_user_func('ErrorHandler::h...', Object(FatalErrorException)) #74 C:\wamp\www\Development\lib\Cake\Error\ErrorHandler.php(203): ErrorHandler::handleFatalError(256, '[CakeException]...', 'C:\\wamp\\www\\Dev...', 128) #75 [internal function]: ErrorHandler::handleError(256, '[CakeException]...', 'C:\\wamp\\www\\Dev...', 128, Array) #76 C:\wamp\www\Development\lib\Cake\Error\ErrorHandler.php(128): trigger_error('[CakeException]...', 256) #77 [internal function]: ErrorHandler::handleException(Object(FatalErrorException)) #78 C:\wamp\www\Development\lib\Cake\Error\ErrorHandler.php(252): call_user_func('ErrorHandler::h...', Object(FatalErrorException)) #79 C:\wamp\www\Development\lib\Cake\Error\ErrorHandler.php(203): ErrorHandler::handleFatalError(256, '[CakeException]...', 'C:\\wamp\\www\\Dev...', 128) #80 [internal function]: ErrorHandler::handleError(256, '[CakeException]...', 'C:\\wamp\\www\\Dev...', 128, Array) #81 C:\wamp\www\Development\lib\Cake\Error\ErrorHandler.php(128): trigger_error('[CakeException]...', 256) #82 [internal function]: ErrorHandler::handleException(Object(FatalErrorException)) #83 C:\wamp\www\Development\lib\Cake\Error\ErrorHandler.php(252): call_user_func('ErrorHandler::h...', Object(FatalErrorException)) #84 C:\wamp\www\Development\lib\Cake\Error\ErrorHandler.php(203): ErrorHandler::handleFatalError(256, '[CakeException]...', 'C:\\wamp\\www\\Dev...', 128) #85 [internal function]: ErrorHandler::handleError(256, '[CakeException]...', 'C:\\wamp\\www\\Dev...', 128, Array) #86 C:\wamp\www\Development\lib\Cake\Error\ErrorHandler.php(128): trigger_error('[CakeException]...', 256) #87 [internal function]: ErrorHandler::handleException(Object(FatalErrorException)) #88 C:\wamp\www\Development\lib\Cake\Error\ErrorHandler.php(252): call_user_func('ErrorHandler::h...', Object(FatalErrorException)) #89 C:\wamp\www\Development\lib\Cake\Error\ErrorHandler.php(203): ErrorHandler::handleFatalError(256, '[CakeException]...', 'C:\\wamp\\www\\Dev...', 128) #90 [internal function]: ErrorHandler::handleError(256, '[CakeException]...', 'C:\\wamp\\www\\Dev...', 128, Array) #91 C:\wamp\www\Development\lib\Cake\Error\ErrorHandler.php(128): trigger_error('[CakeException]...', 256) #92 [internal function]: ErrorHandler::handleException(Object(FatalErrorException)) #93 C:\wamp\www\Development\lib\Cake\Error\ErrorHandler.php(252): call_user_func('ErrorHandler::h...', Object(FatalErrorException)) #94 C:\wamp\www\Development\lib\Cake\Error\ErrorHandler.php(203): ErrorHandler::handleFatalError(256, '[CakeException]...', 'C:\\wamp\\www\\Dev...', 128) #95 [internal function]: ErrorHandler::handleError(256, '[CakeException]...', 'C:\\wamp\\www\\Dev...', 128, Array) #96 C:\wamp\www\Development\lib\Cake\Error\ErrorHandler.php(128): trigger_error('[CakeException]...', 256) #97 [internal function]: ErrorHandler::handleException(Object(FatalErrorException)) #98 C:\wamp\www\Development\lib\Cake\Error\ErrorHandler.php(252): call_user_func('ErrorHandler::h...', Object(FatalErrorException)) #99 C:\wamp\www\Development\lib\Cake\Error\ErrorHandler.php(203): ErrorHandler::handleFatalError(256, '[CakeException]...', 'C:\\wamp\\www\\Dev...', 128) #100 [internal function]: ErrorHandler::handleError(256, '[CakeException]...', 'C:\\wamp\\www\\Dev...', 128, Array) #101 C:\wamp\www\Development\lib\Cake\Error\ErrorHandler.php(128): trigger_error('[CakeException]...', 256) #102 [internal function]: ErrorHandler::handleException(Object(FatalErrorException)) #103 C:\wamp\www\Development\lib\Cake\Error\ErrorHandler.php(252): call_user_func('ErrorHandler::h...', Object(FatalErrorException)) #104 C:\wamp\www\Development\lib\Cake\Error\ErrorHandler.php(203): ErrorHandler::handleFatalError(256, '[CakeException]...', 'C:\\wamp\\www\\Dev...', 128) #105 [internal function]: ErrorHandler::handleError(256, '[CakeException]...', 'C:\\wamp\\www\\Dev...', 128, Array) #106 C:\wamp\www\Development\lib\Cake\Error\ErrorHandler.php(128): trigger_error('[CakeException]...', 256) #107 [internal function]: ErrorHandler::handleException(Object(FatalErrorException)) #108 C:\wamp\www\Development\lib\Cake\Error\ErrorHandler.php(252): call_user_func('ErrorHandler::h...', Object(FatalErrorException)) #109 C:\wamp\www\Development\lib\Cake\Error\ErrorHandler.php(203): ErrorHandler::handleFatalError(256, '[CakeException]...', 'C:\\wamp\\www\\Dev...', 128) #110 [internal function]: ErrorHandler::handleError(256, '[CakeException]...', 'C:\\wamp\\www\\Dev...', 128, Array) #111 C:\wamp\www\Development\lib\Cake\Error\ErrorHandler.php(128): trigger_error('[CakeException]...', 256) #112 [internal function]: ErrorHandler::handleException(Object(FatalErrorException)) #113 C:\wamp\www\Development\lib\Cake\Error\ErrorHandler.php(252): call_user_func('ErrorHandler::h...', Object(FatalErrorException)) #114 C:\wamp\www\Development\lib\Cake\Error\ErrorHandler.php(203): ErrorHandler::handleFatalError(256, '[CakeException]...', 'C:\\wamp\\www\\Dev...', 128) #115 [internal function]: ErrorHandler::handleError(256, '[CakeException]...', 'C:\\wamp\\www\\Dev...', 128, Array) #116 C:\wamp\www\Development\lib\Cake\Error\ErrorHandler.php(128): trigger_error('[CakeException]...', 256) #117 [internal function]: ErrorHandler::handleException(Object(FatalErrorException)) #118 C:\wamp\www\Development\lib\Cake\Error\ErrorHandler.php(252): call_user_func('ErrorHandler::h...', Object(FatalErrorException)) #119 C:\wamp\www\Development\lib\Cake\Error\ErrorHandler.php(203): ErrorHandler::handleFatalError(1, 'Maximum functio...', 'C:\\wamp\\www\\Dev...', 812) #120 [internal function]: ErrorHandler::handleError(1, 'Maximum functio...', 'C:\\wamp\\www\\Dev...', 812, Array) #121 C:\wamp\www\Development\lib\Cake\Core\App.php(931): call_user_func('ErrorHandler::h...', 1, 'Maximum functio...', 'C:\\wamp\\www\\Dev...', 812, Array) #122 C:\wamp\www\Development\lib\Cake\Core\App.php(904): App::_checkFatalError() #123 [internal function]: App::shutdown() #124 {main}

Error: An Internal Error Has Occurred.

qu'est ce que je dois faire pour régler ce problème

jutilise cakephp 2

merci d'avance

Hors ligne

 

#2 09-03-2016 14:30:55

cake17
Cooker
Date d'inscription: 29-03-2012
Messages: 379
Site web

Re: problème avec le plugin uploader

Bonjour,

Je me suis permis de déplacer ton message dans le bon forum.

Ton debug est-il à 2 ?

Hors ligne

 

#3 09-03-2016 15:10:34

koukou
Crêpe Suzette
Date d'inscription: 15-11-2015
Messages: 8

Re: problème avec le plugin uploader

oui il l"est j'ai pu regler ce pb l'erreur existait parce que j'ai pas mis ce code dasn mon controller

Code: php

  1.          if (!empty($this->request->data['Car']['picture1'])) {
  2.          
  3.           $this->Uploader = new Uploader();
  4.           //debug($this->Uploader);die();
  5.       if ($data = $this->Uploader->upload('picture1', array('name' => 'uploaderFilename', 'overwrite' => true))) {
  6.              
  7.         //debug($data);
  8.       }
  9.  
  10.  
  11.     }

mais là j'ai un autre pb l'image n'arrive pas à être uploader ya ce msg : There was an error uploading this file, please try again.
le msg proviens du fichier AttachmentBehaviour.php (le fichier je l'ai téléchargé avec le plugin)
voici le code du fichier

Code: php

  1. <?php
  2. /**
  3.  * AttachmentBehavior
  4.  *
  5.  * A CakePHP Behavior that attaches a file to a model, and uploads automatically, then stores a value in the database.
  6.  *
  7.  * @author      Miles Johnson - http://milesj.me
  8.  * @copyright   Copyright 2006-2011, Miles Johnson, Inc.
  9.  * @license     http://opensource.org/licenses/mit-license.php - Licensed under The MIT License
  10.  * @link        http://milesj.me/code/cakephp/uploader
  11.  */
  12.  
  13. App::uses('Set', 'Utility');
  14. App::uses('String', 'Utility');
  15. App::uses('ModelBehavior', 'Model');
  16.  
  17. App::import('Vendor', 'Uploader.S3');
  18. App::import('Vendor', 'Uploader.Uploader');
  19.  
  20. class AttachmentBehavior extends ModelBehavior {
  21.  
  22.   /**
  23.    * AS3 domain snippet.
  24.    */
  25.   const AS3_DOMAIN = 's3.amazonaws.com';
  26.  
  27.   /**
  28.    * Uploader instance.
  29.    *
  30.    * @access public
  31.    * @var Uploader
  32.    */
  33.   public $uploader = null;
  34.  
  35.   /**
  36.    * S3 instance.
  37.    *
  38.    * @access public
  39.    * @var S3
  40.    */
  41.   public $s3 = null;
  42.  
  43.   /**
  44.    * All user defined attachments; images => model.
  45.    *
  46.    * @access protected
  47.    * @var array
  48.    */
  49.   protected $_attachments = array();
  50.  
  51.   /**
  52.    * Mapping of database columns to form fields.
  53.    *
  54.    * @access protected
  55.    * @var array
  56.    */
  57.   protected $_columns = array();
  58.  
  59.   /**
  60.    * The default settings for attachments.
  61.    *
  62.    * @access protected
  63.    * @var array
  64.    */
  65.   protected $_defaults = array(
  66.     'name' => '',
  67.     'baseDir' => '',
  68.     'uploadDir' => '',
  69.     'append' => '',
  70.     'prepend' => '',
  71.     'dbColumn' => 'uploadPath',
  72.     'importFrom' => '',
  73.     'defaultPath' => '',      // Default file path to be used if the field is empty
  74.     'maxNameLength' => null,
  75.     'overwrite' => false,     // Overwrite a file with the same name if it exists
  76.     'stopSave' => true,       // Stop model save() on form upload error
  77.     'allowEmpty' => true,     // Allow an empty file upload to continue
  78.     'saveAsFilename' => false,    // If true, will only save the filename and not relative path
  79.     'transforms' => array(),
  80.     's3' => array(
  81.       'format' => 'http://{host}/{bucket}/{path}',
  82.       'accessKey' => '',
  83.       'secretKey' => '',
  84.       'ssl' => true,
  85.       'bucket' => '',
  86.       'path' => '',
  87.       'host' => self::AS3_DOMAIN
  88.     ),
  89.     'metaColumns' => array(
  90.       'ext' => '',
  91.       'type' => '',
  92.       'size' => '',
  93.       'group' => '',
  94.       'width' => '',
  95.       'height' => '',
  96.       'filesize' => ''
  97.     )
  98.   );
  99.  
  100.   /**
  101.    * Initialize uploader and save attachments.
  102.    *
  103.    * @access public
  104.    * @param Model $model
  105.    * @param array $config
  106.    * @return void
  107.    */
  108.   public function setup(Model $model, $config = array()) {
  109.     $this->uploader = new Uploader();
  110.  
  111.     if ($config) {
  112.       foreach ($config as $field => $attachment) {
  113.         if (isset($attachment['skipSave'])) {
  114.           $attachment['stopSave'] = $attachment['skipSave'];
  115.         }
  116.  
  117.         $attachment = Set::merge($this->_defaults, $attachment);
  118.         $attachment['field'] = $field;
  119.  
  120.         $columns = array($attachment['dbColumn'] => $field);
  121.  
  122.         if ($attachment['transforms']) {
  123.           foreach ($attachment['transforms'] as $transform) {
  124.             $columns[$transform['dbColumn']] = $field;
  125.           }
  126.         }
  127.  
  128.         $this->_attachments[$model->alias][$field] = $attachment;
  129.         $this->_columns[$model->alias] = $columns;
  130.       }
  131.     }
  132.   }
  133.  
  134.   /**
  135.    * Deletes any files that have been attached to this model.
  136.    *
  137.    * @access public
  138.    * @param Model $model
  139.    * @param boolean $cascade
  140.    * @return mixed
  141.    */
  142.   public function beforeDelete(Model $model, $cascade = true) {
  143.     if (empty($model->id)) {
  144.       return false;
  145.     }
  146.  
  147.     $data = $model->read(null, $model->id);
  148.     $columns = $this->_columns[$model->alias];
  149.  
  150.     if (!empty($data[$model->alias])) {
  151.       foreach ($data[$model->alias] as $column => $value) {
  152.         if (isset($columns[$column])) {
  153.           $attachment = $this->_attachments[$model->alias][$columns[$column]];
  154.  
  155.           $this->uploader->setup($attachment);
  156.           $this->s3 = $this->s3($attachment['s3']);
  157.  
  158.           $path = $attachment['saveAsFilename'] ? rtrim($attachment['uploadDir'], '/') . '/' . $value : $value;
  159.           $this->delete($path);
  160.         }
  161.       }
  162.     }
  163.  
  164.     return true;
  165.   }
  166.  
  167.   /**
  168.    * Before saving the data, try uploading the image, if successful save to database.
  169.    *
  170.    * @access public
  171.    * @param Model $model
  172.    * @return mixed
  173.    */
  174.   public function beforeSave(Model $model, $options = Array()) {
  175.     if (empty($model->data[$model->alias])) {
  176.       return true;
  177.     }
  178.  
  179.     foreach ($model->data[$model->alias] as $field => $file) {
  180.       if (empty($this->_attachments[$model->alias][$field])) {
  181.         continue;
  182.       }
  183.  
  184.       $attachment = $this->_attachments[$model->alias][$field];
  185.       $attachment = $this->callback($model, 'beforeUpload', $attachment);
  186.       $data = array();
  187.  
  188.       // Not a form upload, so lets treat it as an import
  189.       if (is_string($file) && !empty($file)) {
  190.         $attachment['importFrom'] = $file;
  191.       }
  192.  
  193.       // Should we continue if a file threw errors during upload?
  194.       if (empty($file['tmp_name']) || (isset($file['error']) && $file['error'] == UPLOAD_ERR_NO_FILE) || (is_string($file) && empty($attachment['importFrom']))) {
  195.         if ($attachment['stopSave'] && !$attachment['allowEmpty']) {
  196.           return false;
  197.         } else if ($attachment['allowEmpty']) {
  198.           if (empty($attachment['defaultPath'])) {
  199.             unset($model->data[$model->alias][$attachment['dbColumn']]);
  200.           } else {
  201.             $model->data[$model->alias][$attachment['dbColumn']] = $attachment['defaultPath'];
  202.           }
  203.  
  204.           continue;
  205.         }
  206.       }
  207.  
  208.       // Save model method for formatting function
  209.       if (!empty($attachment['name']) && method_exists($model, $attachment['name'])) {
  210.         $attachment['name'] = array($model, $attachment['name']);
  211.       }
  212.  
  213.       // Setup instances
  214.       $this->uploader->setup($attachment);
  215.       $this->s3 = $this->s3($attachment['s3']);
  216.  
  217.       // Upload or import the file and attach to model data
  218.  
  219.       $uploadResponse = $this->upload($file, $attachment, array(
  220.         'overwrite' => $attachment['overwrite'],
  221.         'name' => $attachment['name'],
  222.         'append' => $attachment['append'],
  223.         'prepend' => $attachment['prepend']
  224.       ));
  225.            
  226.       $uploaderOptions = array(
  227.         'uploadDir' => $this->uploader->uploadDir,
  228.         'baseDir' => $this->uploader->baseDir,
  229.         'tempDir' => $this->uploader->tempDir
  230.       );
  231.  
  232.       if (empty($uploadResponse)) {
  233.         $model->invalidate($field, __d('uploader', 'There was an error uploading this file, please try again.'));
  234.         return false;
  235.       }
  236.  
  237.       $basePath = $this->transfer($uploadResponse['path']);
  238.       $data[$attachment['dbColumn']] = ($attachment['saveAsFilename'] && $this->s3 === null) ? basename($basePath) : $basePath;
  239.  
  240.       $toDelete = array();
  241.       $lastPath = $basePath;
  242.  
  243.       // Apply image transformations
  244.       if ($attachment['transforms']) {
  245.         foreach ($attachment['transforms'] as $options) {
  246.           $options['field'] = $field;
  247.           $options = $this->callback($model, 'beforeTransform', $options);
  248.  
  249.           $method = $options['method'];
  250.  
  251.           if (!method_exists($this->uploader, $method)) {
  252.             trigger_error(sprintf('Uploader.Attachment::beforeSave(): "%s" is not a defined transformation method.', $method), E_USER_WARNING);
  253.             return false;
  254.           }
  255.  
  256.           // Apply custom options for transform
  257.           $this->uploader->setup($options);
  258.  
  259.           // Transform image
  260.           $transformResponse = $this->uploader->{$method}($options);
  261.  
  262.           // Rollback uploaded files if one fails
  263.           if (empty($transformResponse)) {
  264.             foreach ($data as $path) {
  265.               $this->delete($path);
  266.             }
  267.  
  268.             $model->invalidate($field, __d('uploader', 'An error occured during image %s transformation.', $method));
  269.             return false;
  270.           }
  271.  
  272.           // Transform successful
  273.           $transformPath = $this->transfer($transformResponse);
  274.           $data[$options['dbColumn']] = ($attachment['saveAsFilename'] && $this->s3 === null) ? basename($transformPath) : $transformPath;
  275.  
  276.           // Delete original if same column name and transform name are not the same file
  277.           if ($options['dbColumn'] == $attachment['dbColumn'] && $lastPath != $transformPath) {
  278.             $toDelete[] = $lastPath;
  279.           }
  280.  
  281.           $lastPath = $transformPath;
  282.  
  283.           // Reset to default settings
  284.           $this->uploader->setup($uploaderOptions);
  285.         }
  286.       }
  287.  
  288.       // Delete old files if replacing them
  289.       if ($toDelete) {
  290.         foreach ($toDelete as $deleteFile) {
  291.           $this->delete($deleteFile);
  292.         }
  293.       }
  294.  
  295.       // Apply meta columns
  296.       if ($attachment['metaColumns']) {
  297.         foreach ($attachment['metaColumns'] as $field => $column) {
  298.           if (isset($uploadResponse[$field]) && !empty($column)) {
  299.             $data[$column] = $uploadResponse[$field];
  300.           }
  301.         }
  302.       }
  303.  
  304.       // Reset S3 and delete original files
  305.       if ($this->s3 !== null) {
  306.         foreach ($this->s3->uploads as $path) {
  307.           $this->delete($path);
  308.         }
  309.  
  310.         $this->s3 = null;
  311.       }
  312.  
  313.       // Merge upload data with model data
  314.       $model->data[$model->alias] = $data + $model->data[$model->alias];
  315.     }
  316.  
  317.     return true;
  318.   }
  319.  
  320.   /**
  321.    * Delete a file from Amazon S3 or locally.
  322.    *
  323.    * @access public
  324.    * @param string $path
  325.    * @return boolean
  326.    */
  327.   public function delete($path) {
  328.     if ($this->s3 !== null && strpos($path, self::AS3_DOMAIN) !== false) {
  329.       return $this->s3->deleteObject($this->s3->bucket, $this->s3->path . basename($path));
  330.     }
  331.  
  332.     return $this->uploader->delete($path);
  333.   }
  334.  
  335.   /**
  336.    * Return an S3 instance.
  337.    *
  338.    * @access public
  339.    * @param array $settings
  340.    * @return S3
  341.    */
  342.   public function s3(array $settings) {
  343.     if (empty($settings['accessKey']) || empty($settings['secretKey'])) {
  344.       return null;
  345.     }
  346.  
  347.     $ssl = isset($settings['useSsl']) ? $settings['useSsl'] : $settings['ssl'];
  348.  
  349.     $s3 = new S3($settings['accessKey'], $settings['secretKey'], (bool) $ssl);
  350.     $s3->host = $settings['host'];
  351.     $s3->bucket = $settings['bucket'];
  352.     $s3->path = trim($settings['path'], '/');
  353.     $s3->format = $settings['format'];
  354.     $s3->uploads = array();
  355.  
  356.     return $s3;
  357.   }
  358.  
  359.   /**
  360.    * Attempt to upload a file via remote import, file system import or standard upload.
  361.    *
  362.    * @access public
  363.    * @param string|array $file
  364.    * @param array $attachment
  365.    * @param array $options
  366.    * @return array
  367.    */
  368.   public function upload($file, $attachment, $options) {
  369.     if ($attachment['importFrom']) {
  370.       if (preg_match('/(http|https)/', $attachment['importFrom'])) {
  371.         return $this->uploader->importRemote($attachment['importFrom'], $options);
  372.  
  373.       } else {
  374.         return $this->uploader->import($attachment['importFrom'], $options);
  375.       }
  376.     }
  377.        
  378.     return $this->uploader->upload($file, $options);
  379.   }
  380.  
  381.   /**
  382.    * Transfer an object to the S3 storage bucket.
  383.    *
  384.    * @access public
  385.    * @param string $path
  386.    * @return string
  387.    */
  388.   public function transfer($path) {
  389.     if ($this->s3 === null) {
  390.       return $path;
  391.     }
  392.  
  393.     $host = empty($this->s3->host) ? self::AS3_DOMAIN : $this->s3->host;
  394.     $name = basename($path);
  395.     $bucket = $this->s3->bucket;
  396.  
  397.     if (!empty($this->s3->path)) {
  398.       $name = $this->s3->path . '/' . $name;
  399.     }
  400.  
  401.     if ($this->s3->putObjectFile($this->uploader->formatPath($path), $bucket, $name, S3::ACL_PUBLIC_READ)) {
  402.       $this->s3->uploads[] = $path;
  403.  
  404.       return String::insert($this->s3->format, array(
  405.         'bucket' => $bucket,
  406.         'path' => $name,
  407.         'host' => $host
  408.       ), array(
  409.         'before' => '{',
  410.         'after' => '}'
  411.       ));
  412.     }
  413.  
  414.     return $path;
  415.   }
  416.  
  417.   /**
  418.    * Trigger a callback function to modify data.
  419.    *
  420.    * @access public
  421.    * @param Model $model
  422.    * @param string $method
  423.    * @param array $options
  424.    * @return array
  425.    */
  426.   public function callback(Model $model, $method, array $options) {
  427.     if (method_exists($model, $method)) {
  428.       return $model->{$method}($options);
  429.     }
  430.  
  431.     return $options;
  432.   }
  433.  
  434. }

merci pour votre aide

Hors ligne

 

Pied de page des forums

Propulsé par FluxBB
Traduction par FluxBB.fr