Tuesday, 14 March 2017

Membuat File Upload Gambar menggunakan script php dan mysql

OK disini saya akan membagikan cara mengupload gambar menggunakan script PHP dan juga MYSQL. langsung saja buat database seperti ini :

 Nama database "mystudy"
 Nama Tabel "upload_image"
 dengan field seperti ini

Setelah Databas dibuat , Pertama-tama kita buat koneksi.php nya dulu

<!-- koneksi.php -->
<?php
$KONEKSI=mysql_connect('localhost' , 'root' , '');
$PILIH=mysql_select_db('mystudy');



//mengkoneksikan ke server

if($PILIH){
echo "Connected";
mysql_error();
}
else{
 die("not Connected");
}
?>


 Sekarang kita Membuat file Index.php nya

<!-- index.php -->

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Membuat Upload File Dengan PHP Dan MySQL</title>
</head>
<body>
<center>
<h1>Membuat Upload File Dengan PHP Dan MySQL</h1>
<form action="tampil.php" method="POST" enctype="multipart/form-data">
<table>
<input type="hidden" name="Id"/>
<tr><td>Image</td><td><input type="file" name="Image"/></td></tr>
<tr><td>Deskripsi</td><td><textarea name="Text" cols="40" rows="4" placeholder="Deskription Images"></textarea></td></tr>
    <tr><td cols="2"><input type="submit" name="Add" value="Add"/>
</table>

</form>
</body>
</html>




setelah membuat file index,php , selanjut nya kita buat file tampil.php karna di action nya juga ke tampil.php

<!-- tampil.php -->

<?php
include ("koneksi.php");
if(isset($_POST["Add"])){
$SQL = mysql_query("insert into upload_image value ('','".$_POST["Text"]."','".$_FILES["Image"]["name"]."')")
 or die("cannot query with database");
  if($SQL){
// the patch to store the upload image
 $tager_dir = "images/";
 $target_file = $tager_dir . $_FILES["Image"]["name"];
      $imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
    // Now let's move the uploaded image into the folder : images
if(move_uploaded_file($_FILES["Image"]["tmp_name"], $target_file)){
echo  "<script>alert ('Upload Sucssesfully ');document.location='' </script>";
}else{
echo  "<script>alert ('Upload Failed ');document.location='' </script>";
}
  }
}
?>
<html>
<body id="top">
<center>
<table border='1'>
<br/>
<br/>
<tr><th>ID</th><th>Image Name</th><th>Deskription</th><th>Images</th><th>Aksi</th></tr>
<?php
$SQL = mysql_query("select * from upload_image");
  while($ROW=mysql_fetch_array($SQL)){
echo '<input type="hidden" name="delImage" value="$ROW["image"];."/>';
echo '<tr><td>'.$ROW["id"].'</td>';
echo '<td>'.$ROW["image"].'</td>';
echo '<td><textarea cols="40" rows="13">'.$ROW["text"].'</textarea></td>';
echo '<td><img src="images/'.$ROW["image"].'" style="width:200px;height:200px"/></td>';
echo '<td><a href="edit.php?edit='.$ROW['id'].'">Edit</a> | <a href="hapus.php?delete='.$ROW['id'].'">Delete</a></td></tr>';
}
?>
</table>
</center>
<br/>
<br/>
<a href="#top"> Kembali ke Atas </a>
<br/>
<br/>
<a href="index.php"> Kembali ke Index </a>
</body>
</html>



sekaran file edit.php nya

<!-- edit.php -->

<?php
include ("koneksi.php");
$ID="";
$TEXT="";
$IMAGE="";
if(isset($_GET["edit"])){
$SQL = mysql_query("select * from upload_image where id='".$_GET["edit"]."'");
 while($ROW=mysql_fetch_array($SQL)){
         $ID=$ROW["id"];
         $TEXT=$ROW["text"];
$IMAGE=$ROW["image"];
 }
}
?>

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Membuat Upload File Dengan PHP Dan MySQL</title>
<style>
.info {
    background-color: #e7f3fe;
    border-left: 6px solid #2196F3;
}
</style>
</head>
<body>
<center>
<h1>Edit Data</h1>
<form action="update.php" method="POST" enctype="multipart/form-data">
<table>
<input type="hidden" name="Id" value="<?php echo $ID;?>"/>
<input type="hidden" name="delImage" value="<?php echo $IMAGE;?>"/>
<tr><td>Image Before</td><td><img src="<?php echo 'images/'.$IMAGE;?>" style="width:200px;height:200px;"/></td></tr>
<tr><td>Image After</td><td><input type="file" name="Image"/></td></tr>
<tr><td>Deskripsi</td><td><textarea name="Text" cols="40" rows="13" placeholder="Deskription Images"><?php echo $TEXT;?></textarea></td></tr>
    <tr><td><input type="submit" name="Update" value="Update"/></td></tr>
</table>
</form>
<div class="info">
  <p><strong>Note :</strong> Kosongkan saja 'Image After' jika anda tidak ingin mengedit data gambar, dan isikan 'Image After' jika anda ingin mengedit data gambar</p>
</div>
<br/>
<br/>
<a href="index.php"> Kembali ke Index </a>
</body>
</html>



update.php

<!-- update.php -->

<?php
include ("koneksi.php");


if(isset($_POST["Update"])){
$filename=$_FILES['Image']['name'];
$move=move_uploaded_file($_FILES['Image']['tmp_name'],'images/'.$filename);
if(empty($filename))   //jika gambar kosong atau tidak di ganti
{
                $update=mysql_query("update upload_image set text='$_POST[Text]' where id='$_POST[Id]' ") or die ("gagal update ");
echo "<script>alert ('data telah di update ');document.location='tampil.php' </script> ";
}
elseif (!empty($filename)) // jika gambar di ganti
{
$delImage = isset($_REQUEST['delImage']) ? $_REQUEST['delImage'] :"";
$dir='images/';

//cek file ekstensi
if (file_exists($dir.$delImage.".jpg")) {
$type = "jpg";
}elseif (file_exists($dir.$delImage.".gif")) {
$type = "gif";
}elseif (file_exists($dir.$delImage.".png")) {
$type = "png";
}elseif (file_exists($dir.$delImage.".jpeg")) {
$type = "jpeg";
}elseif (file_exists($dir.$delImage.".pjpeg")) {
$type = "pjpeg";
}elseif (file_exists($dir.$delImage.".pdf")) {
$type = "pdf";
}else{
$type = "";
}

$ImgOri=$dir.$delImage.".".$type;
unlink($ImgOri);
                $update=mysql_query("update upload_image set text='$_POST[Text]',image='$filename' where id='$_POST[Id]' ") or die ("gagal update gambar ");
echo "<script>alert ('data + gambar telah di update ');document.location='tampil.php' </script> ";
}
}
?>


sekarang hapus.php nya

<!-- hapus.php -->

<?php
include ("koneksi.php");
if(isset($_GET["delete"])){
$SQL = mysql_query ("select * from upload_image where id='".$_GET["delete"]."'");
while ($ROW=mysql_fetch_array($SQL)){
 $IMAGE=$ROW["image"];
}
$dir='images/';

//cek file ekstensi
if (file_exists($dir.$delImage.".jpg")) {
$type = "jpg";
}elseif (file_exists($dir.$delImage.".gif")) {
$type = "gif";
}elseif (file_exists($dir.$delImage.".png")) {
$type = "png";
}elseif (file_exists($dir.$delImage.".jpeg")) {
$type = "jpeg";
}elseif (file_exists($dir.$delImage.".pjpeg")) {
$type = "pjpeg";
}elseif (file_exists($dir.$delImage.".pdf")) {
$type = "pdf";
}else{
$type = "";
}

$ImgOri=$dir.$IMAGE.".".$type;
unlink($ImgOri);
$SQL = mysql_query("delete from upload_image where id='".$_GET["delete"]."'");

 echo "File telah dihapus";
}
include ("tampil.php");
?>


Download script Disini ubah nama folder nya jadi upload
taruh di XAMPP->htdocs->disini
terus panggil di google "localhost/upload/" enter / bisa juga klik file index.php nya langsung

No comments: