Input Method
Seme Framework comes with builtin input manager which can handled $_POST
, $_GET
, and $_REQUEST
.
Here is the example:
class Home extends Sene_Controller{ public function __construct(){ parent::__construct(); } public function index(){ $input_get = $this->input->get('id'); //localhost/seme/?id=100 $input_post = $this->input->post('email'); //handling by form-data post $input_request = $this->input->request('token'); //handling get or post form-data. } }
If the parameter not sent, the default value will return (int) 0
.
Handling $_FILES
For handling $_FILES
you have to created it manually regarding to your application requirements.
Here is the full example for file image upload:
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(SEMEROOT.$targetdir); if (empty($targetdircheck)) { if (PHP_OS == "WINNT") { if (!is_dir(SEMEROOT.$targetdir)) { mkdir(SEMEROOT.$targetdir); } } else { if (!is_dir(SEMEROOT.$targetdir)) { mkdir(SEMEROOT.$targetdir, 0775); } } } $tahun = date("Y"); $targetdir = $targetdir.DIRECTORY_SEPARATOR.$tahun; $targetdircheck = realpath(SEMEROOT.$targetdir); if (empty($targetdircheck)) { if (PHP_OS == "WINNT") { if (!is_dir(SEMEROOT.$targetdir)) { mkdir(SEMEROOT.$targetdir); } } else { if (!is_dir(SEMEROOT.$targetdir)) { mkdir(SEMEROOT.$targetdir, 0775); } } } $bulan = date("m"); $targetdir = $targetdir.DIRECTORY_SEPARATOR.$bulan; $targetdircheck = realpath(SEMEROOT.$targetdir); if (empty($targetdircheck)) { if (PHP_OS == "WINNT") { if (!is_dir(SEMEROOT.$targetdir)) { mkdir(SEMEROOT.$targetdir); } } else { if (!is_dir(SEMEROOT.$targetdir)) { mkdir(SEMEROOT.$targetdir, 0775); } } } $sc->status = 998; $sc->message = 'Invalid file extension uploaded'; if (in_array($fileext, array("gif", "jpg", "png","jpeg"))) { $filecheck = SEMEROOT.$targetdir.DIRECTORY_SEPARATOR.$filename.'.'.$fileext; if (file_exists($filecheck)) { unlink($filecheck); $rand = rand(0, 999); $filename = "$id-$ke-".$rand; $filecheck = SEMEROOT.$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'], SEMEROOT.$targetdir.DIRECTORY_SEPARATOR.$filename); if (is_file(SEMEROOT.$targetdir.DIRECTORY_SEPARATOR.$filename) && file_exists(SEMEROOT.$targetdir.DIRECTORY_SEPARATOR.$filename)) { if (@mime_content_type(SEMEROOT.$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(SEMEROOT.$targetdir.DIRECTORY_SEPARATOR.$filethumb) && is_file(SEMEROOT.$targetdir.DIRECTORY_SEPARATOR.$filethumb)) { unlink(SEMEROOT.$targetdir.DIRECTORY_SEPARATOR.$filethumb); } if (file_exists(SEMEROOT.$targetdir.DIRECTORY_SEPARATOR.$filename) && is_file(SEMEROOT.$targetdir.DIRECTORY_SEPARATOR.$filename)) { WideImage::load(SEMEROOT.$targetdir.DIRECTORY_SEPARATOR.$filename)->reSize(370)->saveToFile(SEMEROOT.$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; }
Requirements
This private method are requires:
- Library Seme_Log
- Library WideImage
- Directory
media/upload/
with write access permission.
public function __construct(){ parent:: __construct(); $this->lib("seme_log"); }
Implementation
Here is the implementation:
$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 .... }