public function upload () { $tmpFile = new TempUploadedFile(); if (array_key_exists("userfile", $_FILES) && file_exists($_FILES["userfile"]["tmp_name"])) { $file = $_FILES['userfile']['tmp_name']; $filename = $_FILES['userfile']['name']; $tmpFile->replace($file); $tmpFile->setOriginalFilename(basename($filename)); } elseif (array_key_exists("url", $_REQUEST)) { $tmpFile->replaceFromURL($_REQUEST["url"]); } else { throw new \Exception("Error: No valid file given"); } PartKeepr::getEM()->persist($tmpFile); PartKeepr::getEM()->flush(); return array("id" => $tmpFile->getId(), "extension" => $tmpFile->getExtension(), "size" => $tmpFile->getSize(), "originalFilename" => $tmpFile->getOriginalFilename()); }
/** * (non-PHPdoc) * @see de\RaumZeitLabor\PartKeepr\Service.RestfulService::create() */ public function create () { $this->requireParameter("tmp_id"); $this->requireParameter("footprint_id"); $tmpImage = TempUploadedFile::loadById($this->getParameter("tmp_id")); $file = new FootprintAttachment(); $footprint = Footprint::loadById($this->getParameter("footprint_id")); $file->setFootprint($footprint); $file->replace($tmpImage->getFilename()); $file->setOriginalFilename($tmpImage->getOriginalFilename()); $file->setDescription($this->getParameter("description")); PartKeepr::getEM()->persist($file); PartKeepr::getEM()->flush(); return $file->serialize(); }
switch ($type) { case "PartAttachment": $file = PartAttachment::loadById($id); break; case "FootprintAttachment": $file = FootprintAttachment::loadById($id); break; default: $file = null; // Add default image? } } catch (\Exception $e) { $file = null; // Something bad happened } if ($file == null) { // Could not find the image, but maybe we want a temporary image? if (array_key_exists("tmpId", $_REQUEST)) { $file = TempUploadedFile::loadById($_REQUEST["tmpId"]); } } header("Content-Type: ".$file->getMimeType()); $fp = fopen($file->getFilename(), "rb"); fpassthru($fp); fclose($fp); PartKeepr::getEM()->flush(); exit();
private function processAttachmentChanges (Part $part, Array $data) { if (array_key_exists("updates", $data)) { foreach ($data["updates"] as $record) { foreach ($part->getAttachments() as $partAttachment) { if ($partAttachment->getId() == $record["id"]) { $partAttachment->setDescription($record["description"]); break; } } } } if (array_key_exists("removals", $data)) { foreach ($data["removals"] as $record) { foreach ($part->getAttachments() as $partAttachment) { if ($partAttachment->getId() == $record["id"]) { PartKeepr::getEM()->remove($partAttachment); $part->getAttachments()->removeElement($partAttachment); break; } } } } if (array_key_exists("inserts", $data)) { foreach ($data["inserts"] as $record) { $attachment = new PartAttachment(); $attachment->setPart($part); $attachment->setDescription($record["description"]); $file = TempUploadedFile::loadById($record["tmp_id"]); $attachment->replace($file->getFilename()); $attachment->setOriginalFilename($file->getOriginalFilename()); $part->getAttachments()->add($attachment); } } }