[X] Choose Font Here

File upload in CodeIgniter

ဒီ Post မွာေတာ့ CI နဲ႔ File/Image Upload တင္တဲ့ အေၾကာင္းကို ေရးမွာ ျဖစ္ပါတယ္။ ကြၽန္ေတာ္ ေရးထားျပီးတဲ့ CI Post

အားလုံးကို ျပန္ဖတ္ခ်င္တယ္ ဆိုရင္ေတာ့

  1. CodeIgniter and MVC for Beginners
  2. Structure and Configuration of CodeIgniter
  3. CodeIgniter Database Library
  4. CodeIgniter Form Helper
  5. Simple Data Entry in CodeIgniter
  6. Pagination with CodeIgniter

 

PHP မွာ File Upload လုပ္တဲ့ concept ကိုေတာ့ သိထားဖို႔  လိုပါတယ္ ခင္ဗ်ာ။ ဒီေတာ့ w3schools ရဲ႕ ဒီ  Article ေလးမွာ ျပန္ၾကည့္ ႏိုင္ပါတယ္။

http://www.w3schools.com/PHP/php_file_upload.asp

အဲဒါကို CI သုံးျပီး ဘယ္လို ျပန္ေရးမလဲ ၾကည့္ရေအာင္ :)

1) Change in config.php and autoload.php

config.php မွာ သင့္ေတာ္သလို ျပင္ေပးျပီး autoload.php မွာလဲ $autoload['helper'] ထဲကို file,form,url ေတြထည့္ ေပးရမယ္။


2) Create View
fileupload_view.php

<html>
    <head>
        <title>FIle Upload Tutorial By CodeIgniter</title>
    </head>
    <body>
        <?
        echo form_open_multipart('fileupload');
        echo form_upload('userfile');
        echo form_submit('upload', 'Upload');
        echo form_close();
        ?>
    </body>
</html>


ဒါကေတာ့ Form helper ကိုသုံးျပီး view တစ္ခုကို create လုပ္တာ ျဖစ္ပါတယ္။ ကြၽန္ေတာ္ Form helper ကိုေရးတုန္းက

ရွင္းထားျပီး ျဖစ္လို႔ usage ေတြကို ထပ္မရွင္းခ်င္ေတာ့ပါ။ ဒီမွာ ျပန္ဖတ္ၾကည့္ႏိုင္ပါတယ္။

3) Create Controller

View ကိုေခၚဖို႔ controller တစ္ခု လုပ္ပါမယ္။ fileupload.php လို႔ လုပ္လိုက္ပါမယ္။ Code ကေတာ့

<?php
class Fileupload extends Controller {
    function index() {
        $this->load->view('fileupload_view');
    }
}

ဒီအဆင့္ ျပီးလို႕ run ၾကည့္ရင္ေတာ့  ဒီလိုျမင္ရမယ္ဗ်ာ။


4) Create Model

file upload လုပ္တဲ့ Code ေတြအားလုံးကို model ထဲမွာမယ္လို႔ စိတ္ကူးလို႕ model တစ္ခုကို create လုပ္တာ ျဖစ္ပါတယ္။

တကယ္လို႕ controller ထဲမွာပဲ ဆက္ခ်င္ရင္လဲ ရတယ္ဗ်ာ။ application နဲ႔ level တူ "upload" folder နဲ႔ အဲဒီေအာက္မွာ

"thumbs" folder ကို create လုပ္ထားဖို႔ လိုပါတယ္။


fileupload_mod.php is here:

<?php
class Fileupload_mod extends Model {

   
    function Fileupload_mod() {
        parent::Model();
    }
   
    function do_fileupload() {
       
        $config['upload_path'] = './upload/';
        $config['allowed_types'] = 'gif|jpg|png';
        $this->load->library('upload', $config);
       
       
        if (!$this->upload->do_upload())
        {
            $error = array('error' => $this->upload->display_errors());
            echo $error;
        }   
        else
        {
            $image_data = $this->upload->data();
        }
       
        $config = array(
            'source_image' => $image_data['full_path'],
            'new_image' => './upload/thumbs',
            'maintain_ration' => true,
            'width' => 150,
            'height' => 100
        );
       
        $this->load->library('image_lib', $config);
        $this->image_lib->resize();
       
    }//do_fileupload

 }

အားလုံးျပီးသြားရင္ေတာ့ controller code ကိုဒီလိုေလး ျပန္ျပင္ ေပးလိုက္ရေအာင္။ model ကိုေခၚထားတဲ့ code ေတြရွိလို႕ ဒီလိုေလး ျပင္လိုက္မွ

perfect ျဖစ္သြားပါမယ္။

<?php
class Fileupload extends Controller {
    function index() {
        $this->load->model('Fileupload_mod');
        if ($this->input->post('upload')) {
            $this->Fileupload_mod->do_fileupload();
        }
        $this->load->view('fileupload_view');
    }
}

ဒါကေတာ့ upload library ကို loading လုပ္ဖို႔ လိုအပ္တဲ့ parameter ေတြကို $config ထဲကို ထည့္ပါတယ္။
ကိုယ့္ requirement အရ လိုအပ္တဲ့ preference variable ေတြကို ဒီဇယားမွာ ၾကည့္ႏိုင္ပါတယ္။

$this->upload->do_upload ဒါဆိုရင္ေတာ့ upload လုပ္သြားျပီဗ်ာ။ error တက္ရင္ေတာ့ error ကို ႐ိုက္ျပမယ္။ error မရွိရင္ေတာ့

thumbnail image တစ္ခုကို ဆက္ျပီး create လုပ္ၾကည့္မယ္။ မျဖစ္မေန လုပ္ရမွာေတာ့ မဟုတ္ပါဘူး။ တကယ္လို႕ ကိုယ့္က image

ကို thumbnail အတြက္ပါ လိုခ်င္တယ္ ဆိုရင္ေတာ့ ဒီလိုလုပ္ေပးရပါမယ္။

$this->load->library('image_lib', $config);
$this->image_lib->resize();

image_lib library ကို load လုပ္ဖို႔ $config variable ထဲကို လိုအပ္တာေတြကို တစ္ခါျပန္ျပီး ထည့္ပါတယ္။ thumbnail လုပ္ဖို႔လိုအပ္တဲ့

'source_image' => $image_data['full_path'], ခုန upload လုပ္ထားတဲ့ image ရဲ႕ path ကိုေျပာတာပါ။

'new_image' => './upload/thumbs',  thumb image ကိုထားမယ့္ ေနရာပါ။

'maintain_ration' => true, original image ရဲ႕ ratio ကိုဆက္ျပီး maintain လုပ္မလုပ္ပါ။

'width' => 150, thumb image ရဲ႕ width

'height' => 100 thumb image ရဲ႕ height

ဒါဆိုရင္ေတာ့ Thumb image ကိုပါ create လုပ္သြားျပီဗ်ာ။ upload နဲ႔ thumb folder ေတြမွာ upload လုပ္ထားတဲ့ image ေတြကို

ၾကည့္ႏိုင္ပါတယ္။ ကြၽန္ေတာ္ လုပ္ထားတဲ့ code ကိုဒီမွာ down ျပီးေတာ့ စမ္းၾကည့္ႏိုင္ပါတယ္။ အဆင္ေျပ ပါေစဗ်ာ။ 


 

Comments (3)
  • luyo

    ကို Zack ရဲ့ ဖိုင္ကိုလဲ ေဒါင္းျပီးစမ္းေတာ့ ဒီပံုစံအတိုင္းပဲ error ျပေနတယ္ image_data ကဘာလို့ Undefined variable ျဖစ္သြားတာလဲမသိဘူး
    အကိုအားရင္တစ္ခ်က္ေလာက္ကူညီေပးပါအံုး
    Array
    A PHP Error was encountered

    Severity: Notice

    Message: Undefined variable: image_data

    Filename: models/fileupload_mod.php

    Line Number: 27

  • admin  - Re:

    အဲဒါ notice ေတြေၾကာင့္ဗ်။ php.ini မွာ error display ကို off လုပ္ထားရင္ ေျပာက္သြားလိမ့္မယ္။
    coding ထဲကေနလဲ error_reporting(0); ဆိုရင္ ရတယ္ဗ်။
    ကြၽန္ေတာ္က error display ကို off လုပ္ထားလို႕ ခင္ဗ် ;)

  • ထြန္းလင္းေအာင္  - Upload Image

    အကို Code project ကို run ၾကည္ရင္ေတာ့ image file က upload ျဖစ္တယ္....ကြ်န္ေတာ္ CodeIgniter မွာကိုယ္တိုင္ေရးၾကည္ရင္ေတာ့ Error တတ္ေနတယ္...
    "You did not select a file to upload".
    အဲ့ဒါဘာလိုလဲး အကို...

Write comment
Your Contact Details:
Comment:
[b] [i] [u] [url] [quote] [code] [img]   
:D:angry::angry-red::evil::idea::love::x:no-comments::ooo::pirate::?::(
:sleep::););)):0
Security
Please input the anti-spam code that you can read in the image.

Login Form

Categories Table View

JoomlaWatch Stats 1.2.9 by Matej Koval

Facebook Share

Share on facebook

Accordion FAQ

mod_joomtouch

Version Iphone

Version Iphone by JoomTouch