/** * createOne * * @param array $data * @throws \Application\Exception * @throws \Bluz\Request\RequestException * @return integer */ public function createOne($data) { /** * Process HTTP File * @var \Bluz\Http\File $file */ $file = Request::getFileUpload()->getFile('file'); if (!$file or $file->getErrorCode() != UPLOAD_ERR_OK) { if ($file->getErrorCode() == UPLOAD_ERR_NO_FILE) { throw new Exception("Please choose file for upload"); } throw new Exception("Sorry, I can't receive file"); } /** * Generate image name */ $fileName = strtolower(isset($data['title']) ? $data['title'] : $file->getName()); // Prepare filename $fileName = preg_replace('/[ _;:]+/i', '-', $fileName); $fileName = preg_replace('/[-]+/i', '-', $fileName); $fileName = preg_replace('/[^a-z0-9.-]+/i', '', $fileName); // If name is wrong if (empty($fileName)) { $fileName = date('Y-m-d-His'); } // If file already exists, increment name $originFileName = $fileName; $counter = 0; while (file_exists($this->uploadDir . '/' . $fileName . '.' . $file->getExtension())) { $counter++; $fileName = $originFileName . '-' . $counter; } // Setup new name and move to user directory $file->setName($fileName); $file->moveTo($this->uploadDir); $this->uploadDir = substr($this->uploadDir, strlen(PATH_PUBLIC) + 1); $data['file'] = $this->uploadDir . '/' . $file->getFullName(); $data['type'] = $file->getMimeType(); $row = $this->getTable()->create(); $row->setFromArray($data); return $row->save(); }
/** * @param array $data * @throws Exception * @throws \Bluz\Request\RequestException * @return integer */ public function createOne($data) { /** @var \Bluz\Http\File $file */ $file = Request::getFileUpload()->getFile('image'); if (!$file or $file->getErrorCode() != UPLOAD_ERR_OK) { if (!$file || $file->getErrorCode() == UPLOAD_ERR_NO_FILE) { throw new Exception('Please choose file to upload'); } throw new Exception('Sorry I can`t receive file'); } $filename = $file->getName(); $name = explode(".", $filename); $name[0] = uniqid($name[0]); $filename = implode(".", $name); $file->setName($filename); $file->moveTo($this->uploadDir); $data['file'] = $this->uploadDir . '/' . $file->getFullName(); $data['type'] = $file->getMimeType(); $row = $this->getTable()->create(); $row->setFromArray($data); return $row->save(); }
/** * @param array $data * @throws Exception * @throws \Bluz\Request\RequestException * @return integer */ public function upload() { /** @var \Bluz\Http\File $file */ $file = Request::getFileUpload()->getFile('files'); $type = $file->getType(); $row = new \Application\MusicianImage\Row(); $row->getTable()->create(); $row->setFromArray(['type' => $type]); $row->beforeSave(); $row->afterSave(); if (!$file or $file->getErrorCode() != UPLOAD_ERR_OK) { if (!$file || $file->getErrorCode() == UPLOAD_ERR_NO_FILE) { throw new Exception('Please choose file to upload'); } throw new Exception('Sorry I can`t receive file'); } $name = uniqid(); $filename = $name . "." . $file->getExtension(); $file->setName($name); $file->moveTo($this->uploadDir); Session::set('image', $filename); return $file; }
public function createOne() { //get saved data $existFilesData = Session::get('files'); $files = unserialize($existFilesData); //get paths to upload directory $path = Config::getModuleData('menu', 'full_path'); $relativePath = Config::getModuleData('menu', 'relative_path'); //get new file,that saved in /tmp directory $newFileData = Request::getFileUpload()->getFile('files'); $editor = new Manager($newFileData, $path); //validate file name $editor->renameFileName(); //merge new and exist files data if ($existFilesData) { $fileObjects = $files; $fileObjects[uniqid()] = $editor->getFile(); } else { $fileObjects = [uniqid() => $editor->getFile()]; } Session::set('files', serialize($fileObjects)); $file = $editor->getFile(); return array('id' => array_search($editor->getFile(), $fileObjects), 'path' => $relativePath . $file->getName() . '.' . $file->getExtension()); }
/** * Test FileUpload with three sub key */ public function testFileUploadWithThreeSubKey() { $_FILES = array('file' => array('name' => array('a' => array('b' => array('test.jpeg', 'test1.jpeg'))), 'size' => array('a' => array('b' => array(filesize($this->path1), filesize($this->path2)))), 'type' => array('a' => array('b' => array('image/jpeg', 'image/jpeg'))), 'tmp_name' => array('a' => array('b' => array($this->path1, $this->path2))), 'error' => array('a' => array('b' => array(0, 0))))); $result = Request::getFileUpload()->getFiles('file[a][b]'); $this->assertNotEmpty($result); }
*/ /** * @namespace */ namespace Application; use Bluz\Config\ConfigException; use Bluz\Http\File; use Bluz\Proxy\Config; use Bluz\Proxy\Request; return function () { /** * @var Bootstrap $this * @var \Bluz\Http\FileUpload $fileUpload */ $fileUpload = Request::getFileUpload(); $file = $fileUpload->getFile('file'); if ($file && $file->getType() == File::TYPE_IMAGE) { // save original name $original = $file->getName(); // rename file to date/time stamp $file->setName(date('Ymd_Hi')); // switch to JSON response $this->useJson(); if (!$this->user()) { throw new Exception('User not found'); } $userId = $this->user()->id; // directory structure: // uploads/ // %userId%/