Skip to content

Instantly share code, notes, and snippets.

@Hillzacky
Last active August 22, 2020 17:08
Show Gist options
  • Save Hillzacky/afef42c6500e6a85003b7aa23fffb053 to your computer and use it in GitHub Desktop.
Save Hillzacky/afef42c6500e6a85003b7aa23fffb053 to your computer and use it in GitHub Desktop.
-- START DATABASE phpMyAdmin
SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET AUTOCOMMIT = 0;
START TRANSACTION;
SET time_zone = "+00:00";
CREATE DATABASE IF NOT EXISTS `db_upload_blob` DEFAULT CHARACTER SET latin1 COLLATE latin1_swedish_ci;
USE `db_upload_blob`;
CREATE TABLE `tb_gambar` (
`id_gambar` int(11) NOT NULL,
`gambar` blob NOT NULL,
`nama_gambar` varchar(255) NOT NULL,
`tipe_gambar` varchar(255) NOT NULL,
`ukuran_gambar` int(11) NOT NULL,
`keterangan` varchar(255) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
ALTER TABLE `tb_gambar`
ADD PRIMARY KEY (`id_gambar`);
ALTER TABLE `tb_gambar`
MODIFY `id_gambar` int(11) NOT NULL AUTO_INCREMENT;
COMMIT;
-- END DATABASE
// koneksi.php
<?php
$host = "localhost";
$user = "root";
$password = "";
$database = "db_upload_blob";
$koneksi = mysqli_connect($host, $user, $password, $database);
?>
// form_upload.php
<?php
include('koneksi.php');
if(isset($_POST['tombol']))
{
if(!isset($_FILES['gambar']['tmp_name'])){
echo '<span style="color:red"><b><u><i>Pilih file gambar</i></u></b></span>';
}
else
{
$file_name = $_FILES['gambar']['name'];
$file_size = $_FILES['gambar']['size'];
$file_type = $_FILES['gambar']['type'];
if ($file_size < 2048000 and ($file_type =='image/jpeg' or $file_type == 'image/png'))
{
// mengkonversi file yang kita upload menjadi data binary dan disimpan di variabel $image
$image = addslashes(file_get_contents($_FILES['gambar']['tmp_name']));
$keterangan = $_POST['keterangan'];
mysqli_query($koneksi,"insert into tb_gambar (gambar,nama_gambar,tipe_gambar,ukuran_gambar,keterangan) values ('$image','$file_name','$file_type','$file_size','$keterangan')");
header("location:index.php");
}
else
{
echo '<span style="color:red"><b><u><i>Ukuruan File / Tipe File Tidak Sesuai</i></u></b></span>';
}
}
}
?>
<html>
<head>
<title></title>
</head>
<body>
<form method="post" action="" enctype="multipart/form-data">
<table>
<tr>
<td>Gambar</td>
<td><input type="file" name="gambar"/></td>
</tr>
<tr>
<td>Keterangan</td>
<td><textarea name="keterangan"></textarea></td>
</tr>
<tr>
<td></td>
<td><input type="submit" name="tombol"/></td>
</tr>
</table>
</form>
</body>
</html>
// delete_gambar.php
<?php
if(isset($_GET['id_gambar']))
{
include('koneksi.php');
$id_gambar = $_GET['id_gambar'];
$query = mysqli_query($koneksi,"delete from tb_gambar where id_gambar='$id_gambar'");
}
header('location:index.php');
?>
// image_view.php
<?php
include('koneksi.php');
if(isset($_GET['id_gambar']))
{
$query = mysqli_query($koneksi,"select * from tb_gambar where id_gambar='".$_GET['id_gambar']."'");
$row = mysqli_fetch_array($query);
header("Content-type: " . $row["tipe_gambar"]);
echo $row["gambar"];
}
else
{
header('location:index.php');
}
?>
// index.php
<?php
include('koneksi.php');
$query = mysqli_query($koneksi,"SELECT * FROM tb_gambar");
?>
<html>
<head>
<title></title>
</head>
<body>
<a href="form_upload.php">Upload Gambar</a>
<table border="1">
<tr>
<th>No</th>
<th>Gambar</th>
<th>Keterangan</th>
<th>Tipe</th>
<th>Ukuran</th>
<th>Action</th>
</tr>
<?php
$no = 1;
while($row = mysqli_fetch_array($query))
{
?>
<tr>
<td><?php echo $no++; ?></td>
<td><img src="image_view.php?id_gambar=<?php echo $row['id_gambar']; ?>" width="100"/></td>
<td><?php echo $row['keterangan']; ?></td>
<td><?php echo $row['tipe_gambar']; ?></td>
<td><?php echo $row['ukuran_gambar']; ?></td>
<td><a href="delete_gambar.php?id_gambar=<?php echo $row['id_gambar']; ?>">Delete</a></td>
</tr>
<?php
}
?>
</table>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment