Skip to content

Instantly share code, notes, and snippets.

@jerry-maheswara-github
Created June 23, 2018 09:58
Show Gist options
  • Save jerry-maheswara-github/5b66ce603170d7b7208054ac73642710 to your computer and use it in GitHub Desktop.
Save jerry-maheswara-github/5b66ce603170d7b7208054ac73642710 to your computer and use it in GitHub Desktop.
<?php
defined('BASEPATH') or exit('No direct script access allowed');
class Upload extends MY_Controller
{
////////////////////////////////////////
public function single()
{
if ($_FILES['user_avatar_file']['error'] == 0) {
/// upload avatar
$upload_path = './assets/img/user';
$ext = strtolower(pathinfo($_FILES['user_avatar_file']['name'], PATHINFO_EXTENSION));
$file_name = 'avatar_' . md5(time() . uniqid()) . '.' . $ext;
//$param['user_avatar'] = 'user/' . $file_name;
$_SESSION['user_info'][0]->user_avatar = 'user/' . $file_name;
if (!is_dir($upload_path)) {
mkdir($upload_path);
}
chmod($upload_path, 0777);
$config_avatar['upload_path'] = $upload_path;
$config_avatar['allowed_types'] = 'png';
$config_avatar['max_width'] = 256;
$config_avatar['max_height'] = 256;
$config_avatar['file_name'] = $file_name;
$config_avatar['overwrite'] = true;
$config_avatar['file_ext_tolower'] = true;
// $config_avatar['max_size'] = 1024;
$this->load->library('upload', $config_avatar, 'upload_avatar');
if (!$this->upload_avatar->do_upload('user_avatar_file')) {
$error['message'] = strip_tags($this->upload_avatar->display_errors());
//opn($error);
//echo 'avatar';
exit();
} else {
$this->db->where('user_id', $user_id);
$cek_logo = $this->db->get('user')->row();
// echo is_file('./files/images/user/'.$cek_logo->user_avatar);
if (is_file('./assets/img/' . $cek_logo->user_avatar)) {
unlink('./assets/img/' . $cek_logo->user_avatar);
}
}
}
}
////////////////////////////////////////
public function multiple(){
foreach ($_FILES as $key => $value) {
if (is_array($value['name'])) {
foreach ($value['name'] as $n_key => $n_value) {
if (!$value['error'][$n_key]) {
$_FILES[$key]['name'] = $value['name'][$n_key];
$_FILES[$key]['type'] = $value['type'][$n_key];
$_FILES[$key]['tmp_name'] = $value['tmp_name'][$n_key];
$_FILES[$key]['error'] = $value['error'][$n_key];
$_FILES[$key]['size'] = $value['size'][$n_key];
$upload_path = './assets/img/item';
$ext = strtolower(pathinfo($_FILES[$key]['name'], PATHINFO_EXTENSION));
$file_name = 'item_images_' . md5(time() . uniqid());
//$param_item_images['item_images_filename'] = 'item/' . $file_name . '.' . $ext;
// $config['upload_path'] = './assets/img/item';
$config['upload_path'] = $upload_path;
$config['allowed_types'] = 'png';
$config['max_width'] = 256;
$config['max_height'] = 256;
$config['file_name'] = $file_name . '.' . $ext;
$config['overwrite'] = true;
$config['file_ext_tolower'] = true;
$this->load->library('upload');
$this->upload->initialize($config);
if (!$this->upload->do_upload($key)) {
// echo $this->upload->display_errors();
$error['message'] = strip_tags($this->upload->display_errors());
//opn($error);
exit();
// $res['status'] = 'error';
// $res['color'] = 'danger';
// $res['message'] = 'Add feature failed.';
} else {
if ($cek_item_images) {
if (is_file('./assets/img/' . $cek_item_images->item_images_filename)) {
unlink('./assets/img/' . $cek_item_images->item_images_filename);
unlink('./assets/img/' . $cek_item_images->item_images_thumbnail);
}
}
/// resize thumbnail
$upload_data = $this->upload->data();
// $image_ext = strtolower(pathinfo($upload_data['full_path'], PATHINFO_EXTENSION));
$image_config['image_library'] = 'gd2';
$image_config['source_image'] = $upload_data['full_path'];
$image_config['quality'] = "100%";
$image_config['maintain_ratio'] = true;
$image_config['width'] = 80;
$image_config['height'] = 80;
$image_config['new_image'] = $upload_data["file_path"] . $file_name . '_thumbnail_.' . $ext;
$this->load->library('image_lib', $image_config);
$this->image_lib->clear();
$this->image_lib->initialize($image_config);
if (!$this->image_lib->resize()) {
echo $this->image_lib->display_errors();exit();
} else {
$param_item_images['item_images_thumbnail'] = 'item/' . $file_name . '_thumbnail_.' . $ext;
}
}
}
}
}
}
}
////////////////////////////////////////
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment