coding

Important Code – PHP, Codeigniter

CSV Delimiter Detection Function

<?php
       function _detectDelimiter($path_file){
        $file1 = fopen($path_file, "r");

        $delimiters = array(
            ';' => 0,
            ',' => 0,
            "\t" => 0,
            "|" => 0
        );

        $firstLine = fgets($file1);
        //fclose($file1); 
        foreach ($delimiters as $delimiter => &$count) {
            $count = count(str_getcsv($firstLine, $delimiter));
        }

        fclose($file1); 
        return array_search(max($delimiters), $delimiters);
    }
    
?>

CSV Read File

<?php

    $delimiter = $this->_detectDelimiter($path_file); //get delimiter
        
    $sts_valid = '';

    $file_open = fopen($path_file, "r");

    $flag = true;
    
    while (($importdatacek = fgetcsv($file_open, 10000, "$delimiter")) !== FALSE){
        
    }	
?>

Handling Image

<?php 
 
    function insert(){
        $data = $this->input->post();
		$id = $this->M_Sarpras->tambah_sarpras($data);
 
        if(!empty($_FILES['image_sarpras']['name'])){ //ingat sesuaikan dengan name nya di input file 

            // handling image
            $path = "/upload/sarpras/";
            $config['upload_path']   = FCPATH.'/upload/sarpras/';
            $config['allowed_types'] = 'jpg|jpeg|png|ico';
            $config['encrypt_name']  = TRUE; 
        
            //upload ke folder
            $this->load->library('upload',$config); 
        
            if($this->upload->do_upload('image_sarpras')){ 
                $jenis ='img';
                $this->_upload_foto($jenis,$id,$path,"tb_sarpras");
            } 
        }  
    }

	function _upload_foto($jenis,$id,$path,$db){
        ini_set('memory_limit', '-1'); //allowed memory >

        $gbr = $this->upload->data();
		$file = FCPATH."$path".$gbr['file_name'];
		  
        $data = getimagesize($file);

        $width = $data[0];
        $height = $data[1];

        $name = $gbr['file_name'];

         //compress dan konfigurasi
         $config['image_library'] = 'gd2';
         $config['source_image']  = FCPATH."$path".$name;
         $config['maintain_ratio']= FALSE;
         $config['quality']       = '50%';
         $config['width']         = $width+1;
         $config['height']        = $height+1;
         //upload ulang ke folder
         $this->load->library('image_lib', $config);
         $this->image_lib->resize();

        //variabel input ke database
        $nama   = $gbr['file_name'];//$this->upload->data('file_name');
		$data1  = array($jenis =>$nama);
		
		$this->db->where('id',$id);
		$this->db->update($db,$data1);
 
    }

    
	function proses_update(){
		$this->staff_auth();
		$data = $this->input->post();
		$this->M_Sarpras->update_sarpras($data);

		if(!empty($_FILES['image_sarpras']['name'])){ //ingat sesuaikan dengan name nya di input file 
			// handling image
			$path = "/upload/sarpras/";
			$config['upload_path']   = FCPATH.'/upload/sarpras/';
			$config['allowed_types'] = 'jpg|jpeg|png|ico';
			$config['encrypt_name']  = TRUE; 
			//upload ke folder
			$this->load->library('upload',$config);

			if($this->upload->do_upload('image_sarpras')){ 
				$id   = $data['id'];
				$img1 = $this->db->query("SELECT img from tb_sarpras where id=$id")->row_array()['img'];
				$jenis ='img';
				$this->_upload_foto($jenis,$data['id'],$path,"tb_sarpras");
				if($img1!=NULL){unlink(FCPATH.'/upload/sarpras/'.$img1);}
			} 
		}  

		redirect('admin/data_sarpras');
	}


?>

Validation function

<?php
    function validasi1($string1,$filter1){
        foreach($filter1 as $filter_word1){
            if(strcasecmp($string1,$filter_word1) == 0){
                return false;
            }
        }
        return true;
    }

    if(validasi1($bln_up,$filter_bln)){
        $sts_valid= 2;
        $ket.= " (Bulan $bln_up pada file tidak sesuai dengan bulan yang dipilih !) ";
    }    

     // must number
     if(!preg_match('/^[0-9]+$/', $dt_15)){  }

    //  validasi format date
    if (!preg_match("/^[0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])$/",$date)){}
?>

PHP Based Function

<?php
    // delete file
    if(file_exists($source)){ 
        @unlink($source);
    };

    // add digit
    $bln = sprintf('%02d', $bln);

    // divide by delimiter
    explode(',', $kot_sel1); 

    // delete last character string
    substr($kot1, 0, -1); 

    // cek lenght
    strlen($str1)
?>

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button
Index