File upload in CodeIgniter
Saturday, 14 August 2010 21:50
ဒီ Post မွာေတာ့ CI နဲ႔ File/Image Upload တင္တဲ့ အေၾကာင္းကို ေရးမွာ ျဖစ္ပါတယ္။ ကြၽန္ေတာ္ ေရးထားျပီးတဲ့ CI Post
အားလုံးကို ျပန္ဖတ္ခ်င္တယ္ ဆိုရင္ေတာ့
-
CodeIgniter and MVC for Beginners
-
Structure and Configuration of CodeIgniter
-
CodeIgniter Database Library
-
CodeIgniter Form Helper
-
Simple Data Entry in CodeIgniter
-
Pagination with CodeIgniter

PHP မွာ File Upload လုပ္တဲ့ concept ကိုေတာ့ သိထားဖို႔ လိုပါတယ္ ခင္ဗ်ာ။ ဒီေတာ့ w3schools ရဲ႕ ဒီ Article ေလးမွာ ျပန္ၾကည့္ ႏိုင္ပါတယ္။
အဲဒါကို CI သုံးျပီး ဘယ္လို ျပန္ေရးမလဲ ၾကည့္ရေအာင္ 
1) Change in config.php and autoload.php
config.php မွာ သင့္ေတာ္သလို ျပင္ေပးျပီး autoload.php မွာလဲ $autoload['helper'] ထဲကို file,form,url ေတြထည့္ ေပးရမယ္။
<html>
2) Create View
fileupload_view.php
<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 ျပီးေတာ့ စမ္းၾကည့္ႏိုင္ပါတယ္။ အဆင္ေျပ ပါေစဗ်ာ။
Share me for Joomla 1.5
Saturday, 14 August 2010 00:09
Joomla မွာ Facebook, Buzz, Twitter စတဲ့ Social Networks ေတြမွာ ကိုယ့္ Article ကို Share လုပ္ခ်င္ေတာ့ ဒီ
Plugin ေလးကေကာင္းတယ္ဗ်။

http://extensions.joomla.org/extensions/social-web/republish/11283
WinXP installation on Mac by VMware Fusion
Thursday, 12 August 2010 15:30
မၾကာခဏ အေမးခံ ေနလို႕ Mac ေပၚမွာ Window XP ကို VMWare Fusion သုံးျပီး Installation လုပ္တဲ့ Youtube video ေလး ျဖစ္ပါတယ္။ ကြၽန္ေတာ္ေတာ့ အဲဒီထဲက အတိုင္း တစ္ဆင့္ျပီး တစ္ဆင့္ လုပ္တာ လုံးဝ အိုေကပါတယ္။ 
More Articles...
Page 4 of 59
«StartPrev12345678910NextEnd»Latest Post
Login Form
Categories Table View
- Reader's Conner (103)
- PHP (45)
- Joomla CMS (36)
- Codeigniter (10)
- jQuery (6)
- PHP & AJAX (4)
- Apache (3)
- For Mac (3)
- mySQL (2)
