Simple PHP GET/POST/FILE/SERVER Class

I made this simple class sometime ago, and is very usefully for checking variable's and assigning defaults if none exist.

It also includes a multiple file upload retriever, which as people might know when you have multiple file uploads, all the files are mixed up a bit in the upload, this basically re-orders them and puts the data into a simple array. I can't take much credit for this, these are basically things I have seen in other systems and made my own class out of them.


  
  
class Request
{

 public static function arrayCheck($key,$array,$default = null) // Key Exists
 {
 if(isset($array[$key]))
  {
  return $array[$key];
  }
 return $default;
 }

 public static function get($var,$value = null) // Retrieve Get
 {
  return self::checkInput($var,$value,$_GET);
 }
 
 public static function post($var,$value = null) // Retrieve Post
 {
  return self::checkInput($var,$value,$_POST);
 }
 
 public static function session($var,$value = null) // Retrieve Session
 {
  return self::checkInput($var,$value,$_SESSION);
 }
 
 public static function file($var,$value = null) // Retrieve Single File
 {
  return self::checkInput($var,$value,$_FILES);
 }
 
 public static function multiFile($name) // Retrive Multiple File Uploads
 {
 $f = $_FILES[$name];
 if(!empty($f))
  {
  $file_ary = array();
  $file_count = count($f['name']);
  $file_keys = array_keys($f);
  for ($i=0; $i<$file_count; $i++)
   {
   foreach ($file_keys as $k)
    {
    $n = array_keys($f[$k])[$i];
    $file_ary[$n][$k] = $f[$k][$n];
    }
   }
  return $file_ary;
  }
 return false; 
 }
 
 public static function server($var,$value = null) // Retrieve Server
 {
  return self::checkInput($var,$value,$_SERVER);
 }

 private static function checkInput($var,$value,&$array) // Is Value Valid in Input
 {
 if(array_key_exists($var,$array)) // Data has been found
  {
  $d = self::checkIsArray($array[$var]); // Return Single Value or Array
  return $d;
  }
 return $value; // Default; (Generally used for Checkbox's)
 }
 
 private static function checkIsArray(&$var)
 {
 if(is_array($var))
  {
   return array_map(array(__CLASS__, 'checkIsArray'), $var); // If Array loop
  }
 return $var;
 }
 
}



  

No comments:

Post a Comment