/** * @param $pNewName * @return void */ public function renameFolder($pNewName) { File::rename($this->pathFile, $pNewName . $this->fileName . "." . $this->fileType); $this->folder = $pNewName; $this->pathFile = $this->folder . $this->fileName . "." . $this->fileType; $this->model_upload->updateById($this->id_upload, array("path_upload" => $this->pathFile)); }
/** * Méthode permettant de redimensionner une image uploadée (enregistrée en base) * http://www.site.com/statique/resize/id:2/w:200/h:200/ * * $_GET["id"] int Id de l'upload * $_GET["w"] int largeur max souhaitée * $_GET["h"] int hauteur max souhaitée * @return void */ public function resize() { if (!Form::isNumeric($_GET["id"]) || !Form::isNumeric($_GET["w"]) || !Form::isNumeric($_GET["h"])) { Go::to404(); } if (!file_exists($image = ModelUpload::getPathById($_GET["id"]))) { Go::to404(); } preg_match(File::REGEXP_EXTENSION, $image, $extract); $ext = $extract[1]; $folder_cache = "includes/applications/" . Core::$application . "/_cache/imgs/"; $file_cache = $folder_cache . "resize_" . $_GET["id"] . "_" . $_GET["w"] . "_" . $_GET["h"] . "." . $ext; if ($app != "main") { Configuration::$server_url .= "../"; } if (file_exists($file_cache)) { Header::location(Configuration::$server_url . $file_cache); } Image::createCopy($image, $file_cache, $_GET["w"], $_GET["h"]); Header::location(Configuration::$server_url . $file_cache); }
/** * Méthode de renommage des fichiers uploadés en fonction de l'id de l'entrée enregistrée * Renvoi le tableau associatif des nouveaux de fichiers pour l'update de la base * @param string|int $pId * @return array */ public function setUploadFileName($pId = null) { foreach ($this->data as $name => &$inp) { if (isset($inp['tag']) && $inp['tag'] == self::TAG_UPLOAD && isset($this->post[$name]) && !empty($this->post[$name])) { if (!isset($inp['fileName']) || !preg_match('/(\\{id\\})/', $inp['fileName'])) { continue; } $folderName = self::PATH_TO_UPLOAD_FOLDER; if (isset($inp['folder'])) { $folderName .= $inp['folder']; } $fileName = preg_replace("/(\\{id\\})/", $pId, $inp["fileName"]); $newPath = $folderName . $fileName; $id_upload = $this->post[$name]; $m = new ModelUpload(); $m->renameById($id_upload, $newPath); } } $newFileName = array(); $max = count($this->uploads); for ($i = 0; $i < $max; ++$i) { $upload = $this->uploads[$i]; $name = $upload["name"]; /** @var Upload $up */ $up = $upload["instance"]; if ($pId == null) { $pId = $up->id_upload; } if ($this->data[$name]["fileName"]) { $fileName = preg_replace("/(\\{id\\})/", $pId, $this->data[$name]["fileName"]); $up->renameFile($fileName); } if (preg_match("/(\\{id\\})/", $this->data[$name]["folder"])) { $folderName = self::PATH_TO_UPLOAD_FOLDER . preg_replace("/(\\{id\\})/", $pId, $this->data[$name]["folder"]); $up->renameFolder($folderName); } } return $newFileName; }
/** * @static * @param int $pId * @return String */ public static function getPathById($pId) { $i = new ModelUpload(); return $i->getValueById("path_upload", $pId); }