/** * Details of an upload */ public function executeFiche(sfWebRequest $r) { $a = array(); // Getting current object $this->u = $this->getRoute()->getObject(); // If not found, send 404 to browser //$this->forward404Unless($this->u); // Calling edit form $formEdit = new UploadsForm($this->u); // If editing if ($r->isMethod('post')) { // Binding fields $formEdit->bind($r->getParameter($formEdit->getName()), $r->getFiles($formEdit->getName())); // If everything is correct if ($formEdit->isValid()) { // Save it ! $formEdit->save(); // Redirect $this->redirect("@uploadcats?c=" . $this->u->Categories->getSlug()); } } // Getting some related content $this->coms = Doctrine::getTable('MsgMessages')->getComments("up", $this->u->getId()); $this->ups = Doctrine::getTable('Uploads')->getUploadsByUser($this->u->getAuthor()); // If torrent, getting related peers if ($this->u->getHash()) { $this->peers = Doctrine_Query::create()->select('p.*, u.username, u.avatar')->from('TorrentsPeers p')->leftJoin('p.Users u')->where('p.hash = ?', $this->u->getHash())->andWhere('p.updated_at > ?', date('Y-m-d H:i:s', time() - 3600))->orderBy('remain')->execute(array(), Doctrine::HYDRATE_ARRAY); foreach ($this->peers as $id => $peer) { // Injecting size of upload $this->peers[$id]['uploadsize'] = $this->u->getSize(); // Don't send IP&PID to browser, keep it for us unset($this->peers[$id]['ip'], $this->peers[$id]['pid'], $this->peers[$id]['peer_id']); } } $a['description'] = $this->getTab("Description", "book_open.png", $this->u->getDescription()); $this->files = $this->u->getFiles($this->u->getUrl()); // Infos $a['infos'] = $this->getTab("Download", "document_info.png", $this->getPartial($this->getModuleName() . '/infos', array('u' => $this->u, 'ups' => $this->ups, 'files' => $this->files))); // Filelist if (is_array($this->files)) { $a['files'] = $this->getTab("Files", "folders.png", $this->files); } else { $a['files'] = $this->getTab("Files", "folders.png", array('url' => $this->u->getUrl())); } // If NFO, getting content if (is_file("uploads/nfo/" . $this->u->getNfo())) { $a['nfo'] = $this->getTab("NFO", "script.png", "<pre>" . htmlentities(file_get_contents("uploads/nfo/" . $this->u->getNfo())) . "</pre>"); } // Comments $f = new UploadsComsForm(); $f->setDefault('upid', $this->u->getId()); $a['coms'] = $this->getTab("Commentaires", "comments.png", $this->coms->toArray()); $a['newcom'] = $this->getTab("Add comment", "comment_add.png", $this->getComponent('messages', 'new', array("form" => $f, "submitUrl" => $this->getContext()->getController()->genUrl($this->getModuleName() . "/comment")))); if (isset($this->peers)) { $a['peers'] = $this->getTab("Peers", "status_online.png", $this->peers); } // If owner or mod or adm, editing mode if ($this->u->getAuthor() == $this->getUser()->getAttribute("id") || $this->getUser()->hasCredential("adm") || $this->getUser()->hasCredential("mod")) { $this->formEdit = $formEdit; $a['opt'] = $this->getTab("Options", "wrench.png", $this->getPartial($this->getModuleName() . '/edit', array("f" => $this->formEdit))); } // Returning content if ($r->isXmlHttpRequest()) { // Sending JSON mime type $this->getResponse()->setHttpHeader('Content-type', 'application/json'); // Sending content return $this->renderText(json_encode(array("right" => $a))); } }
/** * 多文件上传 * @param $name 表单中file的名称 * @param $baseDir 文件的上传路径 */ public static function uploadFiles($name, $baseDir = '') { if ($baseDir == '') { $baseDir = Yii::$app->basePath . '/web/upload/attachment'; } $model = new UploadsForm(); $model->file = UploadedFile::getInstancesByName($name); if (empty($model->file)) { return true; } //没有上传文件 if ($model->validate()) { $resFileArr = []; foreach ($model->file as $f) { $ext = $f->getExtension(); $fileName = md5($f->tempName) . '.' . $ext; $dir = date('Ymd'); $fileDir = $baseDir . '/' . $dir; //var_dump($fileDir); //exit; if (!is_dir($fileDir)) { $res = mkdir($fileDir, 0777, true); if (!$res) { return ['errno' => 10, 'errmsg' => 'create dir failed']; } } $fileUrl = $fileDir . '/' . $fileName; $res = $f->saveAs($fileUrl); if (!$res) { return ['errno' => $model->file->error, 'errmsg' => 'save file failed']; } $resFileArr[] = ['fileUrl' => $dir . '/' . $fileName, 'name' => $f->name]; } } else { return ['errno' => 11, 'errmsg' => json_encode($model->getErrors())]; } //返回文件上传信息 return ['errno' => 0, 'errmsg' => 'success', 'fileInfo' => $resFileArr]; }