Datatypes\Upload\Editor::save PHP Method

save() public method

Save upload editor
public save ( ) : void
return void
    public function save()
    {
        $parameters = $this->getConfig();
        $data = array();
        if (!empty($_FILES[$this->getName()]['name'])) {
            $oldFiles = $_FILES;
            $file = $_FILES[$this->getName()];
            //Ignore others data
            $_FILES = array();
            $_FILES[$this->getName()] = $file;
            $fileClass = new File();
            $fileClass->load($this->getProperty(), $this->getDatatype()->getDocument(), $this->getName());
            $fileClass->upload();
            $files = $fileClass->getFiles();
            if (!empty($files)) {
                foreach ($files as $file) {
                    $name = $file['filename'];
                    $file = $fileClass->getPath() . '/' . $name;
                    if (file_exists($file)) {
                        $const = defined('FILEINFO_MIME_TYPE') ? FILEINFO_MIME_TYPE : FILEINFO_MIME;
                        $finfo = finfo_open($const);
                        // return mimetype extension
                        if (!in_array(finfo_file($finfo, $file), $parameters['mime_list'])) {
                            unlink($file);
                        } else {
                            $fileInfo = @getimagesize($file);
                            $data[] = array('value' => $name, 'width' => empty($fileInfo[0]) ? 0 : $fileInfo[0], 'height' => empty($fileInfo[1]) ? 0 : $fileInfo[1], 'html' => empty($fileInfo[2]) ? '' : $fileInfo[2], 'mime' => empty($fileInfo['mime']) ? '' : $fileInfo['mime']);
                        }
                        finfo_close($finfo);
                    }
                }
                if (empty($parameters['is_multiple']) and !empty($data[0])) {
                    $data = $data[0];
                }
                $data = serialize($data);
            }
            //Restore file data
            $_FILES = $oldFiles;
        } else {
            $data = $this->getRequest()->getPost()->get($this->getName() . '-hidden');
        }
        $this->setValue(empty($data) ? '' : $data);
    }

Usage Example

Example #1
0
 /**
  * Test
  *
  * @return void
  */
 public function testSaveWithWrongMimeType()
 {
     $this->object->setConfig(array('is_multiple' => true, 'mime_list' => array('image/gif', 'image/jpeg', 'image/png')));
     $_FILES = array($this->object->getName() => array('name' => __DIR__ . '/_files/test.bmp', 'type' => 'plain/text', 'size' => 8, 'tmp_name' => __DIR__ . '/_files/test.bmp', 'error' => 0));
     $this->object->save();
     $result = $this->object->getValue();
     $this->removeDirectories();
     $this->assertInternalType('string', $result);
 }