/** * Create a file instance from a file info array ($_FILES) * * @param array $fileInfo * * @return File The created file */ public static function createFromArray(array $fileInfo) { $file = new File(); $file->setName(isset($fileInfo['name']) ? $fileInfo['name'] : null); $file->setSize(isset($fileInfo['size']) ? $fileInfo['size'] : 0); $file->setType(isset($fileInfo['type']) ? $fileInfo['type'] : null); $file->setTmpName(isset($fileInfo['tmp_name']) ? $fileInfo['tmp_name'] : null); $file->setError(isset($fileInfo['error']) ? $fileInfo['error'] : 0); return $file; }
/** * @param File $file */ protected function process($file) { if (!($imageInfo = @getimagesize($file->getUri()))) { throw new ImageFileException('The uploaded file is not an image, or the file was not readable.'); } if (!in_array($imageInfo[2], $this->allowedImageTypes)) { throw new ImageFileException('The image type you uploaded is not allowed. Allowed image types are: ' . implode(', ', $this->{$allowedImageTypes}) . '.'); } $file->setWidth($imageInfo[0]); $file->setHeight($imageInfo[1]); $file->setType($imageInfo[2]); }
public function savePchFileFromRawData(&$pchData, Doctrine_Connection $conn = null) { if ($this->getPchFileId()) { $pchFile = $this->getPchFile(); } else { $pchFile = new File(); } $pchFile->setType('image/pch'); $pchFile->setName('cccc_' . time() . '_pch'); $pchFile->save($conn); $fileBin = $pchFile->getFileBin(); if (!$fileBin) { $fileBin = new FileBin(); $fileBin->setFileId($pchFile->getId); } $fileBin->setBin($pchData); $fileBin->save($conn); $this->setPchFileId($pchFile->getId()); }
public function save_file_data($file_name, $file_type, $file_size, $file_path) { $file_entity = new File(); $base = new Base(); $file_entity->setName($file_name); $file_entity->setSize($file_size); $file_entity->setType($file_type); $file_entity->setPath($file_path); $base->audit_fields($file_entity, 'create'); $file_entity->save(); }
public function executeUpload(sfWebRequest $request) { // for apiKey check $memberId = $this->getUser()->getMember(); if ('1' === $request->getParameter('forceHtml')) { // workaround for some browsers $this->getResponse()->setContentType('text/html'); } if (!$_FILES) { return $this->renderJSON(array('status' => 'error', 'message' => 'null file')); } if (!$_FILES['upfile']) { return $this->renderJSON(array('status' => 'error', 'message' => 'null file')); } $filename = basename($_FILES['upfile']['name']); if (!$filename) { return $this->renderJSON(array('status' => 'error', 'message' => 'null file')); } $tmpFileName = $request->getParameter('changedname'); if ($filename != $tmpFileName) { $separates = $separates = explode('.', $filename); $cnt = count($separates); $fname = ''; $ext = ''; if (1 == $cnt) { $fname = $value; } else { $fname = join('', array_slice($separates, 0, $cnt - 1)); $ext = '.' . $separates[$cnt - 1]; } if ('' == $fname) { $filename = $tmpFileName; } } $filename = preg_replace('/\\|\\/|\\*|:|\\?|\\&|\'|\\"|>|<|undefined|\\|/', '-', urldecode($filename)); $communityId = (int) $request->getParameter('community_id'); if (1 <= (int) $communityId) { $community = Doctrine::getTable('Community')->find($communityId); if (!$community->isPrivilegeBelong($this->getUser()->getMember()->getId())) { return $this->renderJSON(array('status' => 'error', 'message' => 'you are not this community member.')); } $dirname = '/c' . $communityId; } else { $dirname = '/m' . $this->getUser()->getMember()->getId(); } //validate $filepath if (!preg_match('/^\\/[mc][0-9]+/', $dirname)) { return $this->renderJSON(array('status' => 'error', 'message' => 'file path error. ' . $dirname)); } $f = new File(); $f->setOriginalFilename($filename); $f->setType($_FILES['upfile']['type']); $f->setName($dirname . '/' . time() . $filename); $f->setFilesize($_FILES['upfile']['size']); if ($stream = fopen($_FILES['upfile']['tmp_name'], 'r')) { $bin = new FileBin(); $bin->setBin(stream_get_contents($stream)); $f->setFileBin($bin); $f->save(); $response = true; } else { //file open error $response = false; } if (true === $response) { return $this->renderJSON(array('status' => 'success', 'message' => 'file up success ' . $response, 'file' => $f->toArray(false))); } else { return $this->renderJSON(array('status' => 'error', 'message' => 'file upload error')); } }
public function createActivityImageByFileInfoAndActivityId(array $fileInfo, $activityId) { $file = new File(); $file->setOriginalFilename(basename($fileInfo['name'])); $file->setType($fileInfo['type']); $fileFormat = $file->getImageFormat(); if (is_null($fileFormat) || '' == $fileFormat) { $fileFormat = pathinfo($fileInfo['name'], PATHINFO_EXTENSION); } $fileBaseName = md5(time()) . '_' . $fileFormat; $filename = 'ac_' . $fileInfo['member_id'] . '_' . $fileBaseName; $file->setName($filename); $file->setFilesize($fileInfo['size']); $bin = new FileBin(); $bin->setBin($fileInfo['binary']); $file->setFileBin($bin); $file->save(); $activityImage = new ActivityImage(); $activityImage->setActivityDataId($activityId); $activityImage->setFileId($file->getId()); $activityImage->setUri($this->getActivityImageUriByfileInfoAndFilename($fileInfo, $filename)); $activityImage->setMimeType($file->type); $activityImage->save(); $this->createUploadImageFileByFileInfoAndSaveFileName($fileInfo, $filename); return $activityImage; }
protected function getImportedPluginFile($filename, $filepath) { $file = new File(); $file->setType('application/x-gzip'); $file->setOriginalFilename($filename); $file->setName(strtr($filename, '.', '_')); $bin = new FileBin(); $bin->setBin(file_get_contents($filepath)); $file->setFileBin($bin); $file->save(); $this->uploadToS3($file); return $file; }