Example #1
0
 /**
  * Receive the file from the client (Upload)
  *
  * @param  string|array $files (Optional) Files to receive
  * @return bool
  */
 public function receive($files = null)
 {
     if (!$this->isValid($files)) {
         return false;
     }
     $check = $this->_getFiles($files);
     foreach ($check as $file => $content) {
         if (!$content['received']) {
             $table = $this->getTable();
             if ($table === null) {
                 throw new Exception('No table to save set.');
             }
             $filename = $content['name'];
             $rename = $this->getFilter('Rename');
             if ($rename !== null) {
                 $tmp = $rename->getNewName($content['tmp_name']);
                 if ($tmp != $content['tmp_name']) {
                     $filename = $tmp;
                 }
                 $key = array_search(get_class($rename), $this->_files[$file]['filters']);
                 unset($this->_files[$file]['filters'][$key]);
             }
             $filecontent = file_get_contents($content['tmp_name']);
             $filesize = filesize($content['tmp_name']);
             $filemime = Sendlove_Utilities::getMimeType($content['name']);
             $filewidth = $fileheight = 0;
             if (strpos($filemime, 'image') !== false) {
                 list($filewidth, $fileheight) = getimagesize($content['tmp_name']);
             }
             $options = array('app' => $this->getApp(), 'content' => $filecontent, 'content_type' => $filemime, 'filename' => Sendlove_Utilities::createUniqueFilename($filename), 'original_filename' => $filename, 'width' => $filewidth, 'height' => $fileheight, 'size' => $filesize, 'created' => new Zend_Db_Expr('NOW()'), 'updated' => new Zend_Db_Expr('NOW()'));
             if (isset($content['id'])) {
                 $options['id'] = $content['id'];
             }
             if (!($fileid = $this->writeToDatabase($options))) {
                 if ($content['options']['ignoreNoFile']) {
                     $this->_files[$file]['received'] = true;
                     $this->_files[$file]['filtered'] = true;
                     continue;
                 }
                 $this->_files[$file]['received'] = false;
                 return false;
             }
             $this->_files[$file]['id'] = isset($content['id']) ? $content['id'] : $fileid;
         }
         if (!$content['filtered']) {
             if (!$this->_filter($file)) {
                 $this->_files[$file]['filtered'] = false;
                 return false;
             }
             $this->_files[$file]['filtered'] = true;
         }
     }
     return true;
 }