Skip to content

Instantly share code, notes, and snippets.

@teramuza
Created April 6, 2018 01:22
Show Gist options
  • Save teramuza/61bccb227049cf855194a45878a7aaf8 to your computer and use it in GitHub Desktop.
Save teramuza/61bccb227049cf855194a45878a7aaf8 to your computer and use it in GitHub Desktop.
Can't Upload Image(article)
<?php
class Articles extends CI_Controller {
var $session_user;
function __construct() {
parent::__construct();
Utils::no_cache();
if (!$this->session->userdata('logged_in')) {
redirect(base_url('auth/login'));
exit;
}
$this->session_user = $this->session->userdata('logged_in');
$this->load->model('articlesModel');
}
function add(){
$data['title'] = 'Buat Artikel';
$data['session_user'] = $this->session_user;
$this->load->library('upload');
if(count($_POST)){
$config['upload_path'] = './public/images/'; //path folder
$config['allowed_types'] = 'jpg|png|jpeg'; //type yang dapat diakses bisa anda sesuaikan
$config['encrypt_name'] = TRUE; //nama yang terupload nantinya
$this->upload->initialize($config);
if(!empty($_FILES['image']['name'])){
if ($this->upload->do_upload('image'))
{
$img = $this->upload->data();
$image=$img['file_name']; //Mengambil file name dari gambar yang diupload
$data['notif'] = $this->articlesModel->addArticles($this->session_user['users_id'],$image);
}
}else{
$data['notif'] = $this->articlesModel->addArticles($this->session_user['users_id']);
}
}
$this->load->view('includes/header', $data);
$this->load->view('dashboard/article_add', $data);
$this->load->view('includes/footer', $data);
}
}
<?php
class ArticlesModel extends CI_Model{
function __construct(){
parent::__construct();
}
//addArticle
function addArticles($author, $image = ''){
$notif = array();
$data = array(
'article_id' => '',
'author' => $author,
'title' => $this->input->post('title'),
'tags' => $this->input->post('tags'),
'pic' => $image,
'contents' => $this->input->post('contents'),
'is_posted' => $this->input->post('is_posted') ? : 0
);
$this->db->insert('articles', $data);
if ($this->db->affected_rows() > 0) {
$notif['message'] = 'Saved successfully';
$notif['type'] = 'success';
unset($_POST);
} else {
$notif['message'] = 'Something wrong !';
$notif['type'] = 'danger';
}
return $notif;
}
}
<form id="form_validation" method="POST">
<div class="form-group form-float">
<div class="form-line">
<input type="text" class="form-control" name="title" value="<?php echo $this->input->post('title');?>" required>
<label class="form-label">Judul</label>
</div>
</div>
<!-- Tags Input -->
<div class="form-group demo-tagsinput-area">
<div class="form-line">
<p style="color: #aaa">Tags</p>
<input type="text" name="tags" size="150" class="form-control" data-role="tagsinput" value="<?php $this->input->post('tags');?>">
</div>
</div>
<!-- #END# Tags Input -->
<div class="form-group form-float">
<div class="form-line">
<textarea name="contents" cols="20" rows="7" class="form-control " required><?php echo $this->input->post('contents');?></textarea>
<label class="form-label">Isi Artikel</label>
</div>
</div>
<div class="form-group form-float">
<div class="form-line">
<p style="color: #aaa">Gambar</p><img src="" id="pic" style="max-height: 200px;" />
<input type="file" class="form-control" id="imgInp" name="image" accept="image/png,image/jpg,image/jpeg" onchange="document.getElementById('pic').src = window.URL.createObjectURL(this.files[0])"/>
</div>
</div>
<div class="form-group">
<?php
$checked ='';
if(count($_POST)){
if($this->input->post('is_posted')) $checked ='checked';
}
else $checked ='checked';
?>
<input type="checkbox" id="checkbox" name="is_posted" value="1" <?php echo $checked;?>>
<label for="checkbox">Terbitkan</label>
</div>
<button class="btn btn-primary waves-effect" type="submit">SIMPAN</button>
</form>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment