<?php
class Pampa_Util_File {

  public static function getFileArray($label, $index){

    $retorno = array();
    $retorno['name'] =      $_FILES[$label]['name'][$index];
    $retorno['type'] =      $_FILES[$label]['type'][$index];
    $retorno['tmp_name'] =  $_FILES[$label]['tmp_name'][$index];
    $retorno['error'] =     $_FILES[$label]['error'][$index];
    $retorno['size'] =      $_FILES[$label]['size'][$index];

    return $retorno;

  }

  public static function isImageFile($filename){
    $imgs = array("jpg", "jpeg", "gif", "png", "bmp", "tif");

    return array_search(self::getExtension($filename) ,$imgs) !== FALSE;
  }

  public static function getExtension($filename = NULL){
    if(empty($filename)){
      return "";
    }
    $a = array_reverse(explode(".", $filename));

    return strtolower($a[0]);
  }

  public static function getFileSize($filepath, $unit = "Kb"){

    if(!is_file($filepath)){
      return "0".$unit;
    }
    
    $filesize = filesize($filepath);

    switch($unit){
      case "Kb":
        $filesize = $filesize/1024;
        break;
      case "Mb":
        $filesize = $filesize/(1024*1024);
        break;
      case "Gb":
        $filesize = $filesize/(1024*1024*1024);
        break;
    }

    $filesize = number_format($filesize, 2, ",", ".");

    return $filesize.$unit;
  }

  public static function checkPath($path)
  {
    if(!is_dir($path))
    {
      return @mkdir($path, 0777, true);
    }
    else
    {
      if(!is_writable($path))
      {
        if(!chmod($path, 0777))
        {
          return false;
        }
      }
      return true;
    }

  }


}