public function fileuploadAction() { $re = array("returncode" => ReturnCode::$SUCCESS); $request = $this->getRequest(); $user = $this->get('security.context')->getToken()->getUser(); $filename = $request->get("filename"); $circle_id = $request->get("circle_id"); $group_id = $request->get("group_id"); // multipart/form-data try { if (empty($filename) || empty($circle_id) || empty($group_id)) { throw new \Exception("param is null"); } if (!isset($_FILES['userfile'])) { $upfile = tempnam(sys_get_temp_dir(), "we"); unlink($upfile); $somecontent1 = base64_decode($request->get('filedata')); if ($handle = fopen($upfile, "w+")) { if (!fwrite($handle, $somecontent1) == FALSE) { fclose($handle); } } } else { $upfile = $_FILES['userfile']['tmp_name']; } $isimage = preg_match(\Justsy\BaseBundle\Common\MIME::getMIMEImgReg(), strtolower($filename)); $imagefilename_small = preg_replace("/(\\.[^\\.]*)\$/", '_small\\1', $filename); $imagefilename_middle = preg_replace("/(\\.[^\\.]*)\$/", '_middle\\1', $filename); $imagefilepath_small = $upfile . "." . $imagefilename_small; $imagefilepath_middle = $upfile . "." . $imagefilename_middle; $dm = $this->get('doctrine.odm.mongodb.document_manager'); $doc = new \Justsy\MongoDocBundle\Document\WeDocument(); $doc->setName($filename); $doc->setFile($upfile); $dm->persist($doc); $dm->flush(); $fileid = $doc->getId(); $fileid_small = ""; $fileid_middle = ""; if ($isimage) { $im = new \Imagick($upfile); if ($im->getImageWidth() > 100) { if ($im->getImageWidth() >= $im->getImageHeight()) { $im->scaleImage(100, 0); } else { $im->scaleImage(0, 100); } $im->writeImage($imagefilepath_small); $im->destroy(); } else { copy($upfile, $imagefilepath_small); } $im = new \Imagick($upfile); if ($im->getImageWidth() > 400) { $im->scaleImage(400, 0); $im->writeImage($imagefilepath_middle); $im->destroy(); } else { copy($upfile, $imagefilepath_middle); } $doc = new \Justsy\MongoDocBundle\Document\WeDocument(); $doc->setName($imagefilename_small); $doc->setFile($imagefilepath_small); $dm->persist($doc); $dm->flush(); $fileid_small = $doc->getId(); unlink($imagefilepath_small); $doc = new \Justsy\MongoDocBundle\Document\WeDocument(); $doc->setName($imagefilename_middle); $doc->setFile($imagefilepath_middle); $dm->persist($doc); $dm->flush(); $fileid_middle = $doc->getId(); unlink($imagefilepath_middle); } unlink($upfile); //д��ݿ⣺we_files $da = $this->get("we_data_access"); $fixs = explode(".", strtolower($filename)); $sqls = array(); $all_params = array(); $sqls[] = "insert into we_files(`circle_id`,`file_ext`,`file_id`,`file_name`,`post_to_group`,`up_by_staff`,`up_date`)values(?,?,?,?,?,?,now())"; $all_params[] = array((string) $circle_id, (string) $fixs[count($fixs) - 1], (string) $fileid, (string) $filename, (string) $group_id, (string) $user->getUsername()); if ($isimage) { $sqls[] = "insert into we_files_image(file_id, file_id_small, file_id_middle) values(?, ?, ?)"; $all_params[] = array((string) $fileid, (string) $fileid_small, (string) $fileid_middle); } $da->ExecSQLs($sqls, $all_params); $re["returncode"] = ReturnCode::$SUCCESS; $re["file_id"] = $fileid; } catch (\Exception $e) { $re["returncode"] = ReturnCode::$SYSERROR; $this->get('logger')->err($e); } $response = new Response($request->get('jsoncallback') ? $request->get('jsoncallback') . "(" . json_encode($re) . ");" : json_encode($re)); $response->headers->set('Content-Type', 'text/json'); return $response; }
private function getFileId($upfile, $maxwidth = 2100, $maxheight = 1600) { $fileid = ''; if (empty($upfile)) { return ''; } $isimage = preg_match(\Justsy\BaseBundle\Common\MIME::getMIMEImgReg(), strtolower($upfile->getClientOriginalName())); if ($isimage) { if ($maxwidth > 0 && $maxheight > 0) { $im = new \Imagick($upfile->getPathname()); if (!empty($maxwidth) && $im->getImageWidth() > $maxwidth) { unlink($upfile->getPathname()); return ''; } if (!empty($maxheight) && $im->getImageHeight() > $maxheight) { unlink($upfile->getPathname()); return ''; } } //开始上传图片 $dm = $this->get('doctrine.odm.mongodb.document_manager'); $doc = new \Justsy\MongoDocBundle\Document\WeDocument(); $filename = $upfile->getClientOriginalName(); $doc->setName($filename); $doc->setFile($upfile->getPathname()); $dm->persist($doc); $dm->flush(); $fileid = $doc->getId(); } unlink($upfile->getPathname()); return $fileid; }
public function getFileIcon($filename) { return \Justsy\BaseBundle\Common\MIME::getFileIcon($filename); }