Read Detail

UPLOAD CLASS

UPLOAD CLASS dapat dilihat pada file kick/upload/Upload.php. Class ini berfungsi untuk MELAKUKAN UPLOAD FILE DOKUMEN ATAU GAMBAR. Fungsi UPLOAD CLASS adalah sebagai berikut :

1. uploadFile

Fungsi untuk melakukan upload SATU FILE. Fungsi ini mengembalikan nama file yang diupload, jika upload file berhasil.

public function uploadFile($file, $folder, $ext=''){
    $filename = '';
    if($file['name']){
        $t = time();
        $check_ext = $this->validateExtension($file['name'], $ext);
        if($check_ext){
            $filename = $t . "_" . $file['name'];
            move_uploaded_file($file['tmp_name'], UPLOADFOLDER . $folder . "/" . $filename);
        }
    }
    return $filename;
}

2. uploadFiles

Fungsi untuk melakukan upload MULTIPLE FILE.

public function uploadFiles($files, $folder, $ext=''){
    $filename = array();
    foreach($files['name'] as $k=>$v){
        $t = time();
        if($v){
            $check_ext = $this->validateExtension($v, $ext);
            if($check_ext){
                $filename[$k] = $t . "_" . $v;
                move_uploaded_file($files['tmp_name'][$k], UPLOADFOLDER . $folder . "/" . $filename[$k]);
            }
        }
    }
    if(count($filename)){
        $filename = array_values($filename);
    }
    return $filename;
}

3. validateExtension

Fungsi ini sama dengan fungsi pada FILTER CLASS.


Cara memanggil pada CONTROLLER.

//SINGLE FILE UPLOAD
$file = $_FILES;
$fileName = $this->upload->
uploadFile($file['image'], 'news', 'image');
//MULTIPLE FILE UPLOAD
$files = $_FILES;
$fileNames = $this->upload->uploadFiles($files['images'], 'news', 'image');