Mengupload Gambar DI CI - Codeigniter Dan PHP
Catatan
Simpan Data
$photo = $_FILES['photo'];
if($photo=''){}else{
$config['upload_path'] = './assets/uploads';
$config['allowed_types'] = 'jpg|png|gif|tiff';
$this->load->library('upload',$config);
if(!$this->upload->do_upload('photo')){
echo "gagal upload";die();
}else{
$photo = $this->upload->data('file_name');
}
}
$data = array('photo' => $photo);
Untuk Update Data(Edit Data) => Tidak Menggunakan $data array
$photo = $_FILES['userfile']['name'];
if($photo){
$config['upload_path'] = './assets/uploads';
$config['allowed_types'] = 'jpg|png|gif|tiff';
$this->load->library('upload',$config);
if($this->upload->do_upload('userfile')){
$userfile = $this->upload->data('file_name');
$this->db->set('photo',$userfile);
}else{
echo "Gagal Upload"; //atau echo $this->upload->display_errors();
}
}
===================================
buat database db_data
buat table tb_foto
buat field => foto varchar 100
Buat Controller HomeTest.php
public function gambar()
{
$this->load->view('templates/header');
$this->load->view('templates/sidebar');
$this->load->view('test');
$this->load->view('templates/footer');
}
public function gambar_aksi()
{
$gambar = $_FILES['foto']['name'];
if($gambar=''){}
else{
$config['upload_path'] = './uploads/foto'; //kopas aja namanya dari folder
$config['allowed_types'] = 'jpg|png|gif';
$this->load->library('upload',$config);
if(!$this->upload->do_upload('foto')){//foto nama file pada view
echo "upload gagal";die();
}else{
$gambarz = $this->upload->data('file_name');
}
}
$data = array(
'foto' => $gambarz
);
$this->test_model->tambah_foto($data,'tb_foto');
}
Buat Model test_model
<?php
class Test_model extends CI_Model{
public function tambah_foto($data,$table)
{
return $this->db->insert($table,$data);
}
}
?>
Buat View test.php
<div class="container-fluid">
<?php echo form_open_multipart('hometest/gambar_aksi')?>
<div class='form-group'>
<label></label>
<input type="file" name="foto" class="form-control">
</div>
<button type="submit" class="btn btn-primary">Simpan</button>
<?php echo form_close();?>
</div>
====================================================
PHP
Buatlah db_gambar dan tb_gambar field gambar vc 50
Buatlah
1.addGambar.php
<?php
mysql_connect("localhost","root","");
mysql_select_db("db_gambar") or die(mysql_error());
$gambar = $_FILES['gambar']['name'];
$tmp_gambar = $_FILES['gambar']['tmp_name'];
mysql_query("insert into tb_gambar values('$gambar')");
move_uploaded_file($tmp_gambar,"img/".$gambar);
?>
2.index.php
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<form action="addGambar.php" method="post" enctype="multipart/form-data" name="form1" id="form1">
<p> </p>
<p>
<label for="fileField"></label>
<input type="file" name="gambar" id="gambar" />
</p>
<p>
<input type="submit" name="button" id="button" value="Submit" />
</p>
<p> </p>
</form>
</body>
</html>
=======================
Update / Edit Data Diphp
jangan lupa enctype="multipart/form-data"
Edit_data.php
<div class="form-group">
<label>Gambar Barang</label><br>
<img src="../img/<?php echo $data['gambar']?>" style="width:10%;height:10%;">
<input type="file" name="gambar" class="form-control mt-2" value="<?php echo $data['gambar']?>" >
<input type="hidden" value="<?php echo $data['gambar']?>" name="gb" class="form-control" >
</div>
Edit_aksi.php
<?php
Date_default_timezone_set('Asia/Jakarta');
mysql_connect("localhost","root","");
mysql_select_db("db_komputer");
$id = $_POST['id'];
$nabar = $_POST['nabar'];
$harga = $_POST['harga'];
$disc = $_POST['disc'];
$stok = $_POST['stok'];
$berat = $_POST['berat'];
$kondisi = $_POST['kondisi'];
$gb = $_POST['gb'];
$gambar = $_FILES['gambar']['name'];
$tmp_gambar = $_FILES['gambar']['tmp_name'];
$tgl_posting = Date('Y-m-d H:i:s');
$ModifiedDate = Date('Y-m-d H:i:s');
$dt = strlen($gambar);
if($dt==0){
$gb_con = $_POST['gb'];
}else{
$gb_con = $_FILES['gambar']['name'];
$tmp_gb_con = $_FILES['gambar']['tmp_name'];
}
mysql_query("update tb_produk set nabar ='$nabar',harga='$harga',disc='$disc',stok='$stok',berat='$berat',
kondisi='$kondisi',gambar='$gb_con',tgl_posting='$tgl_posting',ModifiedDate='$ModifiedDate' where id='$id'");
move_uploaded_file($tmp_gambar,'../img/'.$gambar);
header("location:edit_sukses_data_barang.php");
?>
Simpan Data
$photo = $_FILES['photo'];
if($photo=''){}else{
$config['upload_path'] = './assets/uploads';
$config['allowed_types'] = 'jpg|png|gif|tiff';
$this->load->library('upload',$config);
if(!$this->upload->do_upload('photo')){
echo "gagal upload";die();
}else{
$photo = $this->upload->data('file_name');
}
}
$data = array('photo' => $photo);
Untuk Update Data(Edit Data) => Tidak Menggunakan $data array
$photo = $_FILES['userfile']['name'];
if($photo){
$config['upload_path'] = './assets/uploads';
$config['allowed_types'] = 'jpg|png|gif|tiff';
$this->load->library('upload',$config);
if($this->upload->do_upload('userfile')){
$userfile = $this->upload->data('file_name');
$this->db->set('photo',$userfile);
}else{
echo "Gagal Upload"; //atau echo $this->upload->display_errors();
}
}
===================================
buat database db_data
buat table tb_foto
buat field => foto varchar 100
Buat Controller HomeTest.php
public function gambar()
{
$this->load->view('templates/header');
$this->load->view('templates/sidebar');
$this->load->view('test');
$this->load->view('templates/footer');
}
public function gambar_aksi()
{
$gambar = $_FILES['foto']['name'];
if($gambar=''){}
else{
$config['upload_path'] = './uploads/foto'; //kopas aja namanya dari folder
$config['allowed_types'] = 'jpg|png|gif';
$this->load->library('upload',$config);
if(!$this->upload->do_upload('foto')){//foto nama file pada view
echo "upload gagal";die();
}else{
$gambarz = $this->upload->data('file_name');
}
}
$data = array(
'foto' => $gambarz
);
$this->test_model->tambah_foto($data,'tb_foto');
}
Buat Model test_model
<?php
class Test_model extends CI_Model{
public function tambah_foto($data,$table)
{
return $this->db->insert($table,$data);
}
}
?>
Buat View test.php
<div class="container-fluid">
<?php echo form_open_multipart('hometest/gambar_aksi')?>
<div class='form-group'>
<label></label>
<input type="file" name="foto" class="form-control">
</div>
<button type="submit" class="btn btn-primary">Simpan</button>
<?php echo form_close();?>
</div>
====================================================
PHP
Buatlah db_gambar dan tb_gambar field gambar vc 50
Buatlah
1.addGambar.php
<?php
mysql_connect("localhost","root","");
mysql_select_db("db_gambar") or die(mysql_error());
$gambar = $_FILES['gambar']['name'];
$tmp_gambar = $_FILES['gambar']['tmp_name'];
mysql_query("insert into tb_gambar values('$gambar')");
move_uploaded_file($tmp_gambar,"img/".$gambar);
?>
2.index.php
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<form action="addGambar.php" method="post" enctype="multipart/form-data" name="form1" id="form1">
<p> </p>
<p>
<label for="fileField"></label>
<input type="file" name="gambar" id="gambar" />
</p>
<p>
<input type="submit" name="button" id="button" value="Submit" />
</p>
<p> </p>
</form>
</body>
</html>
=======================
Update / Edit Data Diphp
jangan lupa enctype="multipart/form-data"
Edit_data.php
<div class="form-group">
<label>Gambar Barang</label><br>
<img src="../img/<?php echo $data['gambar']?>" style="width:10%;height:10%;">
<input type="file" name="gambar" class="form-control mt-2" value="<?php echo $data['gambar']?>" >
<input type="hidden" value="<?php echo $data['gambar']?>" name="gb" class="form-control" >
</div>
Edit_aksi.php
<?php
Date_default_timezone_set('Asia/Jakarta');
mysql_connect("localhost","root","");
mysql_select_db("db_komputer");
$id = $_POST['id'];
$nabar = $_POST['nabar'];
$harga = $_POST['harga'];
$disc = $_POST['disc'];
$stok = $_POST['stok'];
$berat = $_POST['berat'];
$kondisi = $_POST['kondisi'];
$gb = $_POST['gb'];
$gambar = $_FILES['gambar']['name'];
$tmp_gambar = $_FILES['gambar']['tmp_name'];
$tgl_posting = Date('Y-m-d H:i:s');
$ModifiedDate = Date('Y-m-d H:i:s');
$dt = strlen($gambar);
if($dt==0){
$gb_con = $_POST['gb'];
}else{
$gb_con = $_FILES['gambar']['name'];
$tmp_gb_con = $_FILES['gambar']['tmp_name'];
}
mysql_query("update tb_produk set nabar ='$nabar',harga='$harga',disc='$disc',stok='$stok',berat='$berat',
kondisi='$kondisi',gambar='$gb_con',tgl_posting='$tgl_posting',ModifiedDate='$ModifiedDate' where id='$id'");
move_uploaded_file($tmp_gambar,'../img/'.$gambar);
header("location:edit_sukses_data_barang.php");
?>
Mengupload Gambar DI CI - Codeigniter Dan PHP
Reviewed by Leo
on
01:09
Rating:
No comments