[trick] Rest Client Upload File Di PHP/ Codeigniter

Bismillah…

kalo muncul error kayak gini..

Disallowed Key Characters.

class MY_Input extends CI_Input {

    /**
     * Clean Keys
     *
     * This is a helper function. To prevent malicious users
     * from trying to exploit keys we make sure that keys are
     * only named with alpha-numeric text and a few other items.
     * 
     * Extended to allow: 
     *      - '.' (dot), 
     *      - '[' (open bracket),
     *      - ']' (close bracket)
     * 
     * @access  private
     * @param   string
     * @return  string
     */
    function _clean_input_keys($str) {
        // if (!preg_match("/^[a-z0-9:_\/\.\[\]\\s-]+$/i", $str)) {
        if (!preg_match("/^[a-z0-9:_\/-àâçéèêëîôùû]+$|/i", $str)) {

            /**
             * Check for Development enviroment - Non-descriptive 
             * error so show me the string that caused the problem 
             */
            if (getenv('ENVIRONMENT') && getenv('ENVIRONMENT') == 'DEVELOPMENT')
                var_dump($str);
            exit('Disallowed Key Characters.');
        }

        // Clean UTF-8 if supported
        if (UTF8_ENABLED === TRUE) {
            $str = $this->uni->clean_string($str);
        }

        return $str;
    }

}

Simpan di core, dengan nama MY_Input.php….

Kalo udah, ini rest server bisa ditest pake rest client apa ajah … ini yang php:

	function upload_post() {
	 $uploaddir = ROOTPATH.'/foto/full/'; //exit(var_dump($uploaddir));
	    $uploadfile = $uploaddir . basename($_FILES['userfile']['name']);

	    if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
	        $this->response($uploadfile, 200);       
	    } else {
	        $this->response(array('error' => strip_tags($this->upload->display_errors())), 404);        
	    }
	}
	
		$this->load->helper('layout');
		
		$uploadpath = ROOTPATH.'/foto/real/'; //realpath('assets/doc/img/real/');
		$filedata = $_FILES['filedata']['tmp_name'];
		$filename = $_POST['filename'];
		if ($filedata != '' && $filename != '')
		    copy($filedata,$uploadpath.$filename);

		$this->load->library('image_moo');

        // Take The Foto, and IMO WORK!
        $source = $uploadpath.$filename; //exit(dump($source));

        /* SIMPEN FILE */
        $full_des = ROOTPATH."/foto/full/".$filename;          
        $medi_des = ROOTPATH."/foto/medium/".$filename;
        $smal_des = ROOTPATH."/foto/small/".$filename;
        $thum_des = ROOTPATH."/foto/thumbnails/".$filename;

        $medi_h = 342;
        $medi_w = 342;            
        $smal_h = 152;
        $smal_w = 152;
        $thum_h = 62;
        $thum_w = 62;

		$this->image_moo
            // Full
	        ->load($source)
	        ->resize(2000,2000)
	        ->round(10)
	        ->save($full_des,true)
	        
	        // Thumbnails
	        ->load($source)
	        ->stretch($thum_w,$thum_h)
	        ->round(10)
	        ->save($thum_des,true)
	        
	        // Small
	        ->load($source)
	        ->stretch($smal_w,$smal_h)
	        ->round(10)
	        ->save($smal_des,true)
	        
	        // Medium
	        ->load($source)
	        ->stretch($medi_w,$medi_h)
	        ->round(10)
	        ->save($medi_des,true)
        ;

kalo ini yang versi Codeigniter 2.x:

	function upload_post() {
	$status = "";
    $msg 	= "";
     
    if ($status != "error")
    {
        $config['upload_path'] = ROOTPATH.'/foto/full/';
        $config['allowed_types'] = 'gif|jpg|png|doc|txt';
        $config['max_size'] = 1024 * 8;
        $config['encrypt_name'] = FALSE;
 
        $this->load->library('upload', $config);
 
        if (!$this->upload->do_upload('userfile'))
        {
            $status = 'error';
            $msg = $this->upload->display_errors('', '');
        }
        else
        {
            $data = $this->upload->data();
            // $file_id = $this->files_model->insert_file($data['file_name'], $_POST['title']);
            if($data)
            {
                $status = "success";
                $msg = "File successfully uploaded";
            }
            else
            {
                unlink($data['full_path']);
                $status = "error";
                $msg = "Something went wrong when saving the file, please try again.";
            }
        }
        @unlink($_FILES[$file_element_name]);
    }
    echo json_encode(array('status' => $status, 'msg' => $msg));
	}

Udah Kelar!

source:

  1. http://stackoverflow.com/questions/7077666/codeigniter-and-restserver-how-to-upload-images/11163068#11163068
  2. http://toddish.co.uk/blog/ajax-file-upload-in-codeigniter-mvc/
Advertisements
Leave a comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: