/** * Called after the Controller::beforeFilter() and before the controller action * * @param Controller $controller Controller with components to startup * @return void */ public function startup(Controller $controller) { // ファイルアップロード等で post_max_size を超えると $_POSTが空っぽになるため、このタイミングでエラー表示 $contentLength = Hash::get($_SERVER, 'CONTENT_LENGTH'); if ($contentLength > CakeNumber::fromReadableSize(ini_get('post_max_size'))) { $message = __d('files', 'FileUpload.post_max_size.over'); $controller->NetCommons->setFlashNotification($message, array('class' => 'danger', 'interval' => NetCommonsComponent::ALERT_VALIDATE_ERROR_INTERVAL)); $controller->redirect($controller->referer()); } }
/** * Add media file * * @param array $requestData Array of POST data. Will contain form data as well as uploaded files. * * @return bool * @throws CakeException */ public function addMedia(array $requestData) { if (CakeNumber::fromReadableSize(ini_get('post_max_size')) < env('CONTENT_LENGTH')) { throw new CakeException(__d('hurad', 'File could not be uploaded. please increase "post_max_size" in php.ini')); } $this->set($requestData); if ($this->validates()) { $prefix = uniqid() . '_'; $uploadInfo = $requestData['Media']['media_file']; $path = date('Y') . DS . date('m'); if ($uploadInfo['error']) { throw new CakeException($this->getUploadErrorMessages($uploadInfo['error'])); } $folder = new Folder(WWW_ROOT . 'files' . DS . $path, true, 0755); if (!is_writable(WWW_ROOT . 'files')) { throw new CakeException(__d('hurad', '%s is not writable', WWW_ROOT . 'files')); } if (!move_uploaded_file($uploadInfo['tmp_name'], $folder->pwd() . DS . $prefix . $uploadInfo['name'])) { throw new CakeException(__d('hurad', 'File could not be uploaded. Please, try again.')); } $file = new File($folder->pwd() . DS . $prefix . $uploadInfo['name']); $requestData['Media']['user_id'] = CakeSession::read('Auth.User.id'); $requestData['Media']['name'] = $prefix . $uploadInfo['name']; $requestData['Media']['original_name'] = $uploadInfo['name']; $requestData['Media']['mime_type'] = $file->mime(); $requestData['Media']['size'] = $file->size(); $requestData['Media']['extension'] = $file->ext(); $requestData['Media']['path'] = $path; $requestData['Media']['web_path'] = Configure::read('General.site_url') . '/' . 'files' . '/' . $path . '/' . $prefix . $uploadInfo['name']; $this->create(); if ($this->save($requestData)) { return true; } else { return false; } } else { return false; } }
/** * Sets protected properties based on config provided * * @param array $config Configuration array */ public function __construct(array $config = []) { parent::__construct($config); if (!empty($this->_config['path'])) { $this->_path = $this->_config['path']; } if (Configure::read('debug') && !is_dir($this->_path)) { mkdir($this->_path, 0775, true); } if (!empty($this->_config['file'])) { $this->_file = $this->_config['file']; if (substr($this->_file, -4) !== '.log') { $this->_file .= '.log'; } } if (!empty($this->_config['size'])) { if (is_numeric($this->_config['size'])) { $this->_size = (int) $this->_config['size']; } else { $this->_size = CakeNumber::fromReadableSize($this->_config['size']); } } }
/** * Checks the filesize * * @param string|array $check * @param integer|string $size Size in bytes or human readable string like '5MB' * @param string $operator See `Validation::comparison()` * @return boolean Success */ public static function fileSize($check, $operator = null, $size = null) { if (is_array($check) && isset($check['tmp_name'])) { $check = $check['tmp_name']; } if (is_string($size)) { $size = CakeNumber::fromReadableSize($size); } $filesize = filesize($check); return self::comparison($filesize, $operator, $size); }
/** * Sets protected properties based on config provided * * @param array $config Engine configuration * * @return array */ public function config($config = array()) { parent::config($config); if (!empty($config['path'])) { $this->_path = $config['path']; } if (Configure::read('debug') && !is_dir($this->_path)) { mkdir($this->_path, 0775, TRUE); } if (!empty($config['file'])) { $this->_file = $config['file']; if (substr($this->_file, -4) !== '.log') { $this->_file .= '.log'; } } if (!empty($config['size'])) { if (is_numeric($config['size'])) { $this->_size = (int) $config['size']; } else { $this->_size = CakeNumber::fromReadableSize($config['size']); } } return $this->_config; }
/** * Called during validation operations, before validation. Please note that custom * validation rules can be defined in $validate. * * @param array $options Options passed from Model::save(). * @return bool True if validate operation should continue, false to abort * @link http://book.cakephp.org/2.0/en/models/callback-methods.html#beforevalidate * @see Model::save() */ public function beforeValidate($options = array()) { $postMaxSize = CakeNumber::fromReadableSize(ini_get('post_max_size')); $uploadMaxFilesize = CakeNumber::fromReadableSize(ini_get('upload_max_filesize')); $maxUploadSize = CakeNumber::toReadableSize($postMaxSize > $uploadMaxFilesize ? $uploadMaxFilesize : $postMaxSize); $this->validate = Hash::merge($this->validate, array(self::INPUT_NAME => array('uploadError' => array('rule' => array('uploadError'), 'message' => array('Error uploading file'))), 'name' => array('notBlank' => array('rule' => array('notBlank'), 'message' => __d('net_commons', 'Invalid request.'), 'allowEmpty' => false, 'required' => true)), 'slug' => array('notBlank' => array('rule' => array('notBlank'), 'message' => __d('net_commons', 'Invalid request.'), 'allowEmpty' => false, 'required' => true)), 'path' => array('notBlank' => array('rule' => array('notBlank'), 'message' => __d('net_commons', 'Invalid request.'), 'allowEmpty' => false, 'on' => 'create')), 'extension' => array('notBlank' => array('rule' => array('notBlank'), 'message' => __d('net_commons', 'Invalid request.'), 'allowEmpty' => false, 'required' => true)), 'mimetype' => array('notBlank' => array('rule' => array('notBlank'), 'message' => __d('net_commons', 'Invalid request.'), 'allowEmpty' => false)), 'size' => array('numeric' => array('rule' => array('numeric'), 'message' => __d('net_commons', 'Invalid request.'), 'allowEmpty' => false)), 'role_type' => array('notBlank' => array('rule' => array('notBlank'), 'message' => __d('net_commons', 'Invalid request.'), 'allowEmpty' => false, 'required' => true)), 'number_of_downloads' => array('numeric' => array('rule' => array('numeric'), 'message' => __d('net_commons', 'Invalid request.'))), 'status' => array('numeric' => array('rule' => array('numeric'), 'message' => __d('net_commons', 'Invalid request.'))))); return parent::beforeValidate($options); }
/** * edit method * * @return void */ public function add2() { if ($this->request->isPost()) { // $data = $this->data; // if (isset($data['File'][self::INPUT_NAME])) { // $data['File']['name'] = $data['File'][self::INPUT_NAME]['name']; // $data['File']['slug'] = Security::hash($data['File']['name'] . mt_rand() . microtime(), 'md5'); // $data['File']['extension'] = pathinfo($data['File']['name'], PATHINFO_EXTENSION); // $data['File']['original_name'] = $data['File']['slug']; // // if (preg_match('/^image/', $data['File'][self::INPUT_NAME]['type']) === 1 || // preg_match('/^video/', $data['File'][self::INPUT_NAME]['type']) === 1) { // $data['File']['alt'] = $data['File']['name']; // } // $this->FileModel->setUploadPath(self::INPUT_NAME, $data); // } // // if (! $result = $this->FileModel->saveFile($data)) { // //TODO: Error // return; // } // $result[$this->FileModel->alias]['readableSize'] = // CakeNumber::toReadableSize($result[$this->FileModel->alias]['size']); // // $this->renderJson($result); } else { $this->layout = null; $this->set('tabIndex', 0); $postMaxSize = CakeNumber::fromReadableSize(ini_get('post_max_size')); $uploadMaxFilesize = CakeNumber::fromReadableSize(ini_get('upload_max_filesize')); $this->set('maxUploadSize', CakeNumber::toReadableSize($postMaxSize > $uploadMaxFilesize ? $uploadMaxFilesize : $postMaxSize)); $this->set('maxDiskSize', CakeNumber::toReadableSize(1073741824)); $this->set('useDiskSize', CakeNumber::toReadableSize(50000000)); $this->set('data', ['File' => ['role_type' => $this->params->query['roleType'] ? $this->params->query['roleType'] : ''], 'FilesPlugin' => ['plugin_key' => $this->params->query['pluginKey'] ? $this->params->query['pluginKey'] : ''], 'FilesRoom' => ['room_id' => isset($this->params->query['roomId']) ? (int) $this->params->query['roomId'] : 0], 'FilesUser' => ['user_id' => isset($this->params->query['userId']) ? (int) $this->params->query['userId'] : (int) $this->Auth->user('id')]]); $this->set('accept', $this->params->query['accept']); } return; }