Пример #1
0
 protected function addFiles()
 {
     if ($this->data["permission"] < 2) {
         $this->sendFlashMessage("You have not permission to modify pack with ID " . $this->data["pack"]->getId() . ".", "error");
         $this->redirect("/");
     }
     if (isset($_FILES["files"])) {
         for ($i = 0; $i < count($_FILES["files"]["name"]); $i++) {
             $fileName = $_FILES["files"]["name"][$i];
             $fileExtension = pathinfo($fileName, PATHINFO_EXTENSION);
             $fileSize = $_FILES["files"]["size"][$i];
             $fileTmpName = $_FILES["files"]["tmp_name"][$i];
             $fileType = $_FILES["files"]["type"][$i];
             if (!in_array($fileExtension, $this->data["fileExtensionAccept"])) {
                 $this->sendFlashMessage('"' . $fileExtension . '" file extension is not accepted.', "error");
                 $this->redirect("/pack/" . $this->data["pack"]->getId());
             }
             if ($fileSize > 281474943156225.0) {
                 $this->sendFlashMessage("File is too large.", "error");
                 $this->redirect("/pack/" . $this->data["pack"]->getId());
             }
             $file = FileQuery::create()->filterByPack($this->data["pack"])->filterByName($fileName)->findOne();
             if ($file) {
                 $this->sendFlashMessage('File "' . $fileName . '" already exists in this pack.', "error");
                 $this->redirect("/pack/" . $this->data["pack"]->getId());
             }
             $file = new File();
             $f = fopen($fileTmpName, 'r');
             $content = fread($f, filesize($fileTmpName));
             fclose($f);
             $file->setName($fileName);
             if (isset($_POST["description"])) {
                 $file->setDescription($_POST["description"]);
             }
             $file->setType($fileType);
             $file->setSize($fileSize);
             $file->setContent($content);
             $file->setPack($this->data["pack"]);
             if ($file->save() <= 0) {
                 $failures = $file->getValidationFailures();
                 if (count($failures) > 0) {
                     foreach ($failures as $failure) {
                         $this->sendFlashMessage("File has not been added. " . $failure->getMessage(), "error");
                     }
                     $this->redirect("/pack/" . $this->data["pack"]->getId());
                 }
             }
         }
         $this->sendFlashMessage("You have successfuly added new files.", "success");
         $this->redirect("/pack/" . $this->data["pack"]->getId());
     } else {
         setHTTPStatusCode("400");
     }
 }