| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247 |
- <?php
- class file
- {
- public static function record(array $_temp, string $_folderFiles = DIR_DATAS_FILES)
- {
- $md5 = md5_file($_temp["tmp_name"]);
- if(self::findM5($md5) == FALSE){
- if (copy($_temp["tmp_name"], $_folderFiles . $md5)) {
- db::query("INSERT INTO " . DB_T_FILES . " (id, name, size, id_user) VALUES (:id, :name, :size, :id_user)");
- db::bind(':id', $md5);
- db::bind(':name', $_temp['name']);
- db::bind(':size', $_temp['size']);
- db::bind(':id_user', session::getId());
-
- try {
- db::execute();
- return $md5;
- } catch (Exception $ex) {
- unlink($_folderFiles . $md5);
- alert::recError("Erreur #record sur l'import du fichier " . $_temp['name']);
- return FALSE;
- }
- }
- } else {
- alert::recError("Erreur #record ce fichier a déjà été importé : " . $_temp['name']);
- return FALSE;
- }
- }
- public static function getErrorUpload(array $_file){
- $return = [];
- if ($_file['error'] === UPLOAD_ERR_INI_SIZE) {
- $return = [
- "status" => "error",
- "description" => "Le fichier téléchargé dépasse la taille maximale autorisée. Taille maximale : " . ini_get('upload_max_filesize')
- ];
- } elseif ($_file['error'] !== UPLOAD_ERR_OK) {
- $phpFileUploadErrors = array(
- 1 => 'Le fichier téléchargé dépasse la directive upload_max_filesize dans php.ini',
- 2 => 'Le fichier téléchargé dépasse la directive MAX_FILE_SIZE spécifiée dans le formulaire HTML',
- 3 => 'Le fichier n\'a été que partiellement téléchargé',
- 4 => 'Aucun fichier n\'a été téléchargé',
- 6 => 'Il manque un dossier temporaire',
- 7 => 'Échec de l\'écriture du fichier sur le disque',
- 8 => 'Une extension PHP a arrêté le téléchargement du fichier',
- );
- $return = [
- "status" => "error",
- "description" => $phpFileUploadErrors[$_file['error']]
- ];
- } else {
- $return = [
- "status" => "success"
- ];
- }
- return $return;
- }
- public static function findM5(string $_md5)
- {
- db::query("SELECT "
- . "IF(" . DB_T_FILES . ".name IS NOT NULL, TRUE, FALSE) AS exist "
- . "FROM " . DB_T_FILES . " "
- . "WHERE " . DB_T_FILES . ".id = :md5");
- db::bind(':md5', $_md5);
- $return = db::single();
- return ($return == TRUE) ? TRUE : FALSE;
- }
- public static function delete(string $_id = NULL, string $_folderFiles = DIR_DATAS_FILES)
- {
- if (isset($_id) and $_id != NULL and file_exists($_folderFiles . $_id)) {
- if (unlink($_folderFiles . $_id)) {
- db::query("DELETE FROM " . DB_T_FILES . " WHERE id = :id");
- db::bind(':id', $_id);
- try {
- db::execute();
- return TRUE;
- } catch (Exception $ex) {
- alert::recError("Erreur lors de la désindexation du fichier");
- return FALSE;
- }
- } else {
- alert::recError("Erreur sur la suppression du fichier");
- return FALSE;
- }
- } else {
- return FALSE;
- }
- }
- public static function download(string $_id, string $_folderFiles = DIR_DATAS_FILES)
- {
- if (file_exists($_folderFiles . $_id)) {
- return $_folderFiles . $_id;
- } else {
- return FALSE;
- }
- }
- public static function cleanFilesByOrder(string $_path, int $_nbFiles = 5)
- {
- $return = TRUE;
- $cpt = 0;
- $files = array();
- $dir = new DirectoryIterator($_path);
- $blackList = array(
- ".",
- "..",
- "index.html",
- "index.php"
- );
- foreach ($dir as $fileinfo) {
- $files[$fileinfo->getMTime()] = $fileinfo->getFilename();
- }
- krsort($files);
- foreach ($files as $file) {
- if (!in_array($file, $blackList)) {
- if ($cpt++ >= $_nbFiles) {
- $return = (unlink($_path . $file)) ? TRUE : FALSE;
- }
- }
- }
- return $return;
- }
- public static function cleanAllFiles(string $_path)
- {
- $return = TRUE;
- $dir = new DirectoryIterator($_path);
- $blackList = array(
- ".",
- "..",
- "index.html",
- "index.php"
- );
- foreach ($dir as $fileinfo) {
- if (!in_array($fileinfo->getFilename(), $blackList)) {
- $return = (unlink($_path . $fileinfo->getFilename())) ? TRUE : FALSE;
- }
- }
- return $return;
- }
- public static function cleanFilesByTime(string $_path, int $_limitTime = 24 * 3600) // 24*3600 pour une journée
- {
- if ($handle = opendir($_path)) {
- while (false !== ($file = readdir($handle))) {
- $filelastmodified = filemtime($_path . $file);
- if ((time() - $filelastmodified) > $_limitTime) {
- unlink($_path . $file);
- }
- }
- closedir($handle);
- }
- }
- public static function copyFolder(string $_folder, string $_target)
- {
- if ($dir = opendir($_folder)) {
- mkdir($_target);
- while (($file = readdir($dir))) {
- if (($file != '.') && ($file != '..')) {
- if (is_dir($_folder . '/' . $file)) {
- self::copyFolder($_folder . '/' . $file, $_target . '/' . $file);
- } else {
- copy($_folder . '/' . $file, $_target . '/' . $file);
- }
- }
- }
- closedir($dir);
- }
- }
- public static function deleteFolder(string $_dir)
- {
- if (is_dir($_dir)) {
- $command = "rm -r " . $_dir;
- try {
- system($command);
- return TRUE;
- } catch (Exception $ex) {
- return FALSE;
- }
- }
- }
- public static function zip(string $_dir, string $_name)
- {
- $zip = new ZipArchive();
- $zip_name = $_name . ".zip";
- $zip->open($zip_name, ZipArchive::CREATE);
- $files = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($_dir), RecursiveIteratorIterator::LEAVES_ONLY);
- foreach ($files as $file) {
- if (!$file->isDir()) {
- $filePath = $file->getRealPath();
- $relativePath = substr($filePath, strlen($_dir));
- $zip->addFile($filePath, $relativePath);
- }
- }
- $zip->close();
- return $zip_name;
- }
- public static function unzip(string $_zip, string $_target)
- {
- if (is_file($_zip)) {
- $zipInfo = pathinfo($_zip);
- if ($zipInfo["extension"] == "zip") {
- $command = "unzip -q " . $_zip . " -d " . $_target . $zipInfo["filename"];
- try {
- system($command);
- return $zipInfo["filename"];
- } catch (Exception $ex) {
- return FALSE;
- }
- }
- }
- return FALSE;
- }
- public static function sizeFolder(string $_rep)
- {
- $Racine = opendir($_rep);
- $Taille = 0;
- while ($Dossier = readdir($Racine)) {
- if ($Dossier != '..' and $Dossier != '.') {
- //Ajoute la taille du sous dossier
- if (is_dir($_rep . '/' . $Dossier)) $Taille += self::sizeFolder($_rep . '/' .
- $Dossier);
- //Ajoute la taille du fichier
- else $Taille += filesize($_rep . '/' . $Dossier);
- }
- }
- closedir($Racine);
- return $Taille;
- }
- }
|