파일업로드(upload) Sample

<?php
/**
 * Upload Class
 *
 * Created on 2011. 11. 15.
 * @author 불의회상 <hoksi2k@hanmail.net>
 * @package library
 * @subpackage controllers
 * @version 1.0
 */
class Upload_lib extends CI_Controller {
	function __construct() {
		parent::__construct();
		
		// Form, Url Helper Load
		$this->load->helper('form');
		
		// Uload Config 설정
		$config = array(
			'upload_path' => FCPATH . 'sample/upload/',
			'allowed_types' => 'gif|jpg|png',
			'max_size' => '100'
		);
		
		$this->load->library('upload', $config);
	}
	
	function index() {
		$this->_view();
	}
	
	function do_upload() {
		if (!$this->upload->do_upload()) {
			$data = array('error' => $this->upload->display_errors(), 'upload_data' => '');
		} else {
			$data = array('upload_data' => $this->upload->data(), 'error' => '');
			unlink($data['upload_data']['full_path']); // 실제 업로드 로직 구현 할때는 이 라인을 삭제 하세요.
		}

		$this->_view($data);
	}
	
	function custom_upload() {
		// Upload Config 재설정
		$my_config = array(
			'upload_path' => FCPATH . 'sample/upload/',
			'allowed_types' => 'gif|jpg|png',
			'max_size' => '100',
			'max_width' => '1024',
			'max_height' => '768',
			'encrypt_name' => TRUE
		);
		
		// Upload init
		$this->upload->initialize($my_config);
		// Do Upload
		$this->do_upload();
	}
	
	function _view($data = '') {
		$data = $data == '' ? array('error' => '', 'upload_data' => '') : $data;
		$this->load->view('upload_lib_sample', $data);
	}
}
<?php $this->load->view('inc/header')?>


<hr />
Upload Test
<hr />
<?php echo form_open_multipart('upload_lib/do_upload');?>
<table class="tablesorter" border="0" cellpadding="0" cellspacing="1">
<thead>
<tr>
	<th style="text-align:center" width="20%">구분</th>
	<th style="text-align:center"></th>
</tr>
</thead>
<tr>
	<td style="text-align:center">이미지</td>
	<td><input type="file" name="userfile" size="20" />(gif|jpg|png)</td>
</tr>
<tr>
	<td colspan="2" style="text-align:center"><input type="submit" value="이미지 upload" /></td>
</tr>
</table>
</form>

<hr />
Upload Class 설정 변경 Test
<hr />
<?php echo form_open_multipart('upload_lib/custom_upload');?>
<table class="tablesorter" border="0" cellpadding="0" cellspacing="1">
<thead>
<tr>
	<th style="text-align:center" width="20%">구분</th>
	<th style="text-align:center"></th>
</tr>
</thead>
<tr>
	<td style="text-align:center">이미지</td>
	<td><input type="file" name="userfile" size="20" />(gif|jpg|png)</td>
</tr>
<tr>
	<td colspan="2" style="text-align:center"><input type="submit" value="이미지 upload" /></td>
</tr>
</table>
</form>

<?php if($upload_data != '' || $error):?>
<hr/>
업로드 결과
<hr/>
<table class="tablesorter" border="0" cellpadding="0" cellspacing="1">
<thead>
<tr>
	<th style="text-align:center" width="20%">구분</th>
	<th style="text-align:center">Data</th>
</tr>
</thead>
<?php if($upload_data != ''):?>
<?php foreach($upload_data as $item => $value):?>
<tr>
	<td><?php echo $item;?></td>
	<td><?php echo $value;?></td>
</tr>
<?php endforeach; ?>
<?php else:?>
<tr>
	<td style="color: red">Error</td>
	<td style="color: red"><?php echo $error;?></td>
</tr>
<?php endif;?>
</table>
<?php endif;?>

<?php $this->load->view('inc/footer')?>

© Copyright by hoksi(Page rendered in 0.0046 seconds)