Input Property

The $input property from SENE_Controller is an object from SENE_Input class.

SENE_Input

The SENE_Input class purpose is for handling $_POST, $_GET, and $_REQUEST input.

Class Synopsis

Here is the class synopsis of SENE_Input.

class SENE_Input {
  ...
  public function post(string $k, $d = 0){ ... }
  public function get(string $k, $d = 0){ ... }
  public function request(string $k, $d = 0){ ... }
  ...
}

Get method

The get method purpose is to get value from $_GET array using its keyname. If the keyname value will return 0 as default value. Start from Seme Framework 4, the default value can be override in the second parameter.

Basic Usage

This is the basic usage get method from $input property on SENE_Controller .

$this->input->get($keyname [, $default_value=0]) : mixed

Parameters

This method has 2 parameter, there is $keyname and $default_value.

$keyname

This value must match with same key that represented in $_GET[$keyname] global variable.

$default_value

The default return value if the $keyname is not found. This parameter added from Seme Framework 4.

Example

Here is the example get method in a controller class.

class Product extends SENE_Controller{
  public function __construct(){
    parent::__construct();
  }
  ...
  public function detail(){
    ...
    //example URL Request, localhost/seme_framework/product/detail/?id=100
    $product_id = $this->input->get('id');
    ....
  }
  ...
}

Post Method

The post method purpose is to get value from $_POST array using its keyname. If the keyname value will return 0 as default value. Start from Seme Framework 4, the default value can be override in the second parameter.

Basic Usage

This is the basic usage post method from $input property on SENE_Controller .

$this->input->post($keyname [, $default_value=0]) : mixed

Parameters

This method has 2 parameter, there is $keyname and $default_value.

$keyname

This value must match with same key that represented in $_POST[$keyname] global variable.

$default_value

The default return value if the $keyname is not found. This parameter added from Seme Framework 4.

Example

Here is the example post method in a controller class.

class Product extends SENE_Controller{
  public function __construct(){
    parent::__construct();
  }
  ...
  public function submit(){
    ...
    //example URL Request, localhost/seme_framework/product/submi
    //get form html example below
    $name = $this->input->post('name');
    $is_available = $this->input->post('is_available');
    ....
  }
  ...
}
HTML Form Example

here is the HTML form example for testing with controller example.

...
<form action="<?=base_url('product/submit')?>" method="post" enctype="multipart/form-data">
  <input type="text" name="name" />
  <select name="is_available">
    <option value="1">in stock</option>
    <option value="0">out of stock</option>
  </select>
</form>
...

Request method

The request method purpose is to get value from $_REQUEST array using its keyname. If the keyname value will return 0 as default value. Start from Seme Framework 4, the default value can be override in the second parameter.

Basic Usage

This is the basic usage request method from $input property on SENE_Controller .

$this->input->request($keyname [, $default_value=0]) : mixed

Parameters

The request method has 2 parameter, there is $keyname and $default_value.

$keyname

This value must match with same key that represented in $_REQUEST[$keyname] global variable.

$default_value

The default return value if the $keyname is not found. This parameter added from Seme Framework 4.

Request Method Example

Here is the example request method in a controller class.

class Review extends SENE_Controller{
  public function __construct(){
    parent::__construct();
  }
  ..
  public function submit(){
    ...
    $product_id = $this->input->request('product_id');
    ...
  }
  ...
}

Handling File Upload

For handling $_FILES file upload you have to created it manually regarding to your application requirements.

Prerequisited

Before implementing the example, here is the list of prerequisited library:

  • Library SEME_Log,
  • Library WideImage,
  • Directory media/upload/ with write access permission.
...
public function __construct(){
  parent:: __construct();
  //loading the required libraries
  $this->lib("seme_log");
  $this->lib("wideimage", 'inc');
}
..

Create private method for upload image

This is full example for upload image and then create thumbnail using WideImage for image resizer.

/**
* Method for handling file upload
* Only allowed .png, .jpeg, .jpg, .gif extension
* Max file size 2000000 bytes
* Unsupported WebP encoding image
* @param       string $keyname           the $_FILES key that send from html
* @param       string $id                the id of user, product, or uniqid
* @param       string $ke                sequencer
* @constructor
* @return      object                    result with object, contain status, message, image, and thumb.
*/
private function __uploadImagex($keyname, $id, $ke="")
{
  $sc = new stdClass();
  $sc->status = 500;
  $sc->message = 'Error';
  $sc->image = '';
  $sc->thumb = '';
  if (isset($_FILES[$keyname]['name'])) {
    if ($_FILES[$keyname]['size']>2000000) {
      $sc->status = 301;
      $sc->message = 'Ukuran gambar terlalu besar. Silakan pilih dengan ukuran kurang dari 2 MB';
      $this->seme_log->write('User::__uploadImagex -- forceClose '.$sc->status.' '.$sc->message);
      return $sc;
    }
    $filenames = pathinfo($_FILES[$keyname]['name']);
    if (isset($filenames['extension'])) {
      $fileext = strtolower($filenames['extension']);
    } else {
      $fileext = 'jpg';
    }

    if (!in_array($fileext, array("jpg","png","jpeg","gif"))) {
      $sc->status = 303;
      $sc->message = 'Invalid file extension, please try other image file.';
      $this->seme_log->write('User::__uploadImagex -- forceClose '.$sc->status.' '.$sc->message);
      return $sc;
    }
    $filename = "$id-$ke";
    $filethumb = $filename.'-thumb';

    $targetdir = 'media/upload/';
    $targetdircheck = realpath(SENEROOT.$targetdir);
    if (empty($targetdircheck)) {
      if (PHP_OS == "WINNT") {
        if (!is_dir(SENEROOT.$targetdir)) {
          mkdir(SENEROOT.$targetdir);
        }
      } else {
        if (!is_dir(SENEROOT.$targetdir)) {
          mkdir(SENEROOT.$targetdir, 0775);
        }
      }
    }

    $tahun = date("Y");
    $targetdir = $targetdir.DIRECTORY_SEPARATOR.$tahun;
    $targetdircheck = realpath(SENEROOT.$targetdir);
    if (empty($targetdircheck)) {
      if (PHP_OS == "WINNT") {
        if (!is_dir(SENEROOT.$targetdir)) {
          mkdir(SENEROOT.$targetdir);
        }
      } else {
        if (!is_dir(SENEROOT.$targetdir)) {
          mkdir(SENEROOT.$targetdir, 0775);
        }
      }
    }

    $bulan = date("m");
    $targetdir = $targetdir.DIRECTORY_SEPARATOR.$bulan;
    $targetdircheck = realpath(SENEROOT.$targetdir);
    if (empty($targetdircheck)) {
      if (PHP_OS == "WINNT") {
        if (!is_dir(SENEROOT.$targetdir)) {
          mkdir(SENEROOT.$targetdir);
        }
      } else {
        if (!is_dir(SENEROOT.$targetdir)) {
          mkdir(SENEROOT.$targetdir, 0775);
        }
      }
    }

    $sc->status = 998;
    $sc->message = 'Invalid file extension uploaded';
    if (in_array($fileext, array("gif", "jpg", "png","jpeg"))) {
      $filecheck = SENEROOT.$targetdir.DIRECTORY_SEPARATOR.$filename.'.'.$fileext;
      if (file_exists($filecheck)) {
        unlink($filecheck);
        $rand = rand(0, 999);
        $filename = "$id-$ke-".$rand;
        $filecheck = SENEROOT.$targetdir.DIRECTORY_SEPARATOR.$filename.'.'.$fileext;
        if (file_exists($filecheck)) {
          unlink($filecheck);
          $rand = rand(1000, 99999);
          $filename = "$id-$ke-".$rand;
        }
      }
      $filethumb = $filename."-thumb.".$fileext;
      $filename = $filename.".".$fileext;

      move_uploaded_file($_FILES[$keyname]['tmp_name'], SENEROOT.$targetdir.DIRECTORY_SEPARATOR.$filename);
      if (is_file(SENEROOT.$targetdir.DIRECTORY_SEPARATOR.$filename) && file_exists(SENEROOT.$targetdir.DIRECTORY_SEPARATOR.$filename)) {
        if (@mime_content_type(SENEROOT.$targetdir.DIRECTORY_SEPARATOR.$filename) == 'image/webp') {
          $sc->status = 302;
          $sc->message = 'WebP image format currently unsupported';
          $this->seme_log->write('User::__uploadImagex -- forceClose '.$sc->status.' '.$sc->message);
          return $sc;
        }

        $this->lib("wideimage/WideImage", 'wideimage', "inc");
        if (file_exists(SENEROOT.$targetdir.DIRECTORY_SEPARATOR.$filethumb) && is_file(SENEROOT.$targetdir.DIRECTORY_SEPARATOR.$filethumb)) {
          unlink(SENEROOT.$targetdir.DIRECTORY_SEPARATOR.$filethumb);
        }
        if (file_exists(SENEROOT.$targetdir.DIRECTORY_SEPARATOR.$filename) && is_file(SENEROOT.$targetdir.DIRECTORY_SEPARATOR.$filename)) {
          WideImage::load(SENEROOT.$targetdir.DIRECTORY_SEPARATOR.$filename)->reSize(370)->saveToFile(SENEROOT.$targetdir.DIRECTORY_SEPARATOR.$filethumb);
          $sc->status = 200;
          $sc->message = 'Successful';
          $sc->thumb = str_replace("//", "/", $targetdir.'/'.$filethumb);
          $sc->image = str_replace("'\'", "/", $targetdir.'/'.$filename);
          $sc->image = str_replace("//", "/", $targetdir.'/'.$filename);
        } else {
          $sc->status = 997;
          $sc->message = 'Failed: file image not exists';
          $this->seme_log->write('User::__uploadImagex -- forceClose '.$sc->status.' '.$sc->message);
        }
      } else {
        $sc->status = 999;
        $sc->message = 'Upload file failed';
        $this->seme_log->write('User::__uploadImagex -- forceClose '.$sc->status.' '.$sc->message);
      }
    } else {
      $sc->status = 998;
      $sc->message = 'Invalid file extension uploaded';
      $this->seme_log->write('User::__uploadImagex -- forceClose '.$sc->status.' '.$sc->message);
    }
  } else {
    $sc->status = 988;
    $sc->message = 'Keyname file does not exists';
    $this->seme_log->write('User::__uploadImagex -- forceClose '.$sc->status.' '.$sc->message);
  }
  return $sc;
}

Example Implementation

Here is the example implementation of file upload using the private method. On this example, the $keyname value is foto also this $keyname value should be send by multipart/form-data on HTML form or API Request like curl.

...
$sc = $this->__uploadImagex("foto",$id,"1");
if(!is_object($sc)) $sc = new stdClass();
if(!isset($sc->status)) $sc->status=0;
if(!isset($sc->message)) $sc->message='no response from upload processor';
if($sc->status == 200){
  //success
  ....

}else{
  //failed
  ....
}
...