Hauptmenü

Werkzeuge

Kategorien

Archiv

Verzeichnisse mit Inhalt und Unterverzeichnissen rekursiv löschen mit PHP

Erstellt in PHP am 25. September 2019 vom Daschmi

Mit folgender Klasse/Funktion können mit der Programmiersprache PHP Verzeichisse und darin enthaltene Dateien rekursiv gelöscht werden.

abstract class DirHelper {

   /**
    * Verzeichnis mit Inhalten recursiv löschen
    * @param string $path
    * 
    * @return bool
    */
   public static function rmDir(string $path) : bool {
         
      if (is_dir($path)) {
               
         $hPath = opendir($path);
               
         if ($hPath) {
                  
            while ($file = readdir($hPath)) {
                  
               if ($file != "." && $file != "..") {
                        
                  if (!is_dir($path."/".$file)) {
                           
                     unlink($path."/".$file);
               
                  } else {
                  
                     delete_directory($path.'/'.$file);
            
                  }

               }
         
            }
                  
            closedir($hPath);
               
         }
               
         rmdir($path);
               
         return true;
               
      }
            
      return false;
            
   }
   
}

Aufruf

 DirHelper:: rmDir('{Pfad}');

Kleine Hilfsfunktion um FILETIME in einen Unix TIMESTAMP zu konvertieren und umgekehrt

Erstellt in PHP am 6. August 2019 vom Daschmi

Für ein Projekt, bei der eine Kommunikation mit einem Windows Programm und einem Webserver stattfindet musste ich die im Windows bereich gebräuchlichen FILETIME Datumstypen (64Bit) in normale Unix TIMESTAMPS konvertieren. Folgende kleine Funktionen haben die Zeitstempel für mich konvertiert.

abstract Class TimestampHelper {

   public static function FILETIME_to_UNIXTIMESTAMP(float $filetime) : int {
            
      return $filetime / (10 * 1000 * 1000) - 11644473600;
            
   }
         
   public static function UNIXTIMESTAMP_to_FILETIME($timestamp) : int {
      
      return ($timestamp + 11644473600) * (10 * 1000 * 1000);
      
   }

}

Canvas und Rechtecke mit abgerundeten Ecken

Erstellt in Allgemein am 31. Juli 2019 vom Daschmi

Für den Etikator brauchte ich eine kleine Funktion, die in der HTML5 Visualisierung runde Ecken darstellt. Diese habe ich mit dem Canvas Element und folgender Funktion realisiert:

function roundRect(ctx, x, y, w, h, r) {
   
   ctx.beginPath();
   ctx.moveTo(x + r, y);
   ctx.lineTo(x + w - r, y);
   ctx.quadraticCurveTo(x + w, y, x + w, y + r);
   ctx.lineTo(x + w, y + h - r);
   ctx.quadraticCurveTo(x + w, y + h, x + w - r, y + h);
   ctx.lineTo(x + r, y + h);
   ctx.quadraticCurveTo(x, y + h, x, y + h - r);
   ctx.lineTo(x, y + r);
   ctx.quadraticCurveTo(x, y, x + r, y);
   ctx.closePath();

}

PHP Funktion um HEX Farben in Dezimal RGB Farben zu zerlegen

Erstellt in PHP am 18. März 2019 vom Daschmi

/**
 * @param string $hex #FF0000 oder FF0000
 * @param array $default
 * @return array
 */
public static function hex2rgb(string $hex, array $default = ['r' => 0, 'g' => 0, 'b' => 0]) : array {

  if (substr($hex, 0, 1) !== '#') $hex = '#'.$hex;
  if (!preg_match('/#([a-f]|[A-F]|[0-9]){3}(([a-f]|[A-F]|[0-9]){3})?\b/', $hex)) return $default;

  $r = substr($hex, 1, 2); $r = hexdec($r);			
  $g = substr($hex, 3, 2); $g = hexdec($g);
  $b = substr($hex, 5, 2); $b = hexdec($b);

  return [
    'r' => $r,
    'g' => $g,
    'b' => $b
  ];

}