Buscar

Aplikasi Upload Foto/Image di PHP

Selamat sore teman-teman blogger..Sehat kan semua?
Hmmm..sambil menunggu teman yang masih sibuk, saya iseng-iseng bongkar aplikasi sederhana yang ada di file saya, yang isinya terdapat cara upload foto pribadi yang biasanya digunakan untuk biodata seseorang. 
Tak lain dan tak bukan masih menggunakan bahasa pemograman kesayangan yaitu PHP dan dibantu MySQL. Hmm..langsung saja ya..
1. Create tabel biodata (nama terserah teman-teman semua)
CREATE TABLE IF NOT EXISTS `tbl_biodata` (
  `id_user` int(9) NOT NULL AUTO_INCREMENT,
  `nama_depan` varchar(50) NOT NULL,
  `nama_belakang` varchar(50) NOT NULL,
  `email` varchar(50) NOT NULL,
  `password` varchar(50) NOT NULL,
  `kelamin` enum('pria','wanita') NOT NULL,
  `photo` varchar(100) NOT NULL,
  `ukuran` int(12) NOT NULL,
  `type` varchar(100) NOT NULL,
  PRIMARY KEY (`id_user`)
);
2. Membuat Form Input [index.php]
<?php
include "koneksi.php";
if(isset($_POST['submit'])){

 $nama_depan=addslashes(htmlentities(ucwords($_POST['nama_depan'])));
 $nama_belakang=addslashes(htmlentities(ucwords($_POST['nama_belakang'])));
 $email=addslashes(htmlentities($_POST['email']));
 $password=$_POST['password'];
 $kelamin=$_POST['kelamin'];

 $nama_photo=$_FILES['photo']['name'];
 $type = $_FILES['photo']['type'];
 $ukuran=$_FILES['photo']['size'];
 
 if(empty($nama_depan) || empty($nama_belakang) || empty($email) || empty($password) || empty($nama_photo)){
  echo "<script language=\"Javascript\">\n";
  echo "confirmed = window.confirm('Maaf, Form belum lengkap!!');";
  echo "</script>";
  
 }else{  
  $query_email=mysql_query("select * from tbl_biodata where email='$email'");
  $cek=mysql_num_rows($query_email);
  if($cek>0){
   echo "<script language=\"Javascript\">\n";
   echo "confirmed = window.confirm('Maaf, Email sudah dipakai!!');";
   echo "</script>";
  }else{
   if($type != "image/gif"  &&  $type != "image/jpg"  && $type != "image/jpeg" && $type != "image/png") {
    echo "<script language=\"Javascript\">\n";
    echo "confirmed = window.confirm('File Yang Di izinkan Hanya jpg,jpeg,png,gif!!');";
    echo "</script>";
   }else{
    if($ukuran>1000000){
     echo "<script language=\"Javascript\">\n";
     echo "confirmed = window.confirm('File Yang Di izinkan Hanya berukuran kurang dari 1MB!!');";
     echo "</script>";
    }else{
     $uploaddir='./photo/';
     $rnd=date(His);    
     $nama_file_upload=$rnd.'-'.$nama_photo;
     $alamatfile=$uploaddir.$nama_file_upload;
     
     if (move_uploaded_file($_FILES['photo']['tmp_name'],$alamatfile))
     {
      $query=mysql_query("insert into tbl_biodata(nama_depan,nama_belakang,email,password,kelamin,photo,ukuran,type) 
         values('$nama_depan','$nama_belakang','$email','$password','$kelamin','$nama_file_upload','$ukuran','$type')");
       
      if($query){
       echo "<script language=\"Javascript\">\n";
       echo "confirmed = window.confirm('Data Anda berhasil disimpan');";
       echo "</script>";
      }else{
       echo mysql_query();
      }
     }else{
       echo "<script language=\"Javascript\">\n";
       echo "confirmed = window.confirm('Proses upload gagal');";
       echo "</script>";
      echo "<p>kode error = " . $_FILES['location']['error']."</p>";
     }
    }
   }
  }
 }
}else{
 unset($_POST['submit']);
}
?>

<html>
<head>
 <title>Biodata</title>
</head>
<body>
 <form action="" enctype="multipart/form-data" method="post" name="postform">      
 <table width="42%" border="0"  style="font-family:Verdana; size:6px; border-bottom:solid 1px #000000; border-right:solid 1px #000000; border-top:solid 1px #000000; border-left:solid 1px #000000; background:#999" align="center" cellpadding="3" cellspacing="3">
    <tr>
    <td colspan="3" align="center"><strong>FORM INPUT BIODATA</strong></td>
  </tr>
    <tr>
  <tr>
    <td width="146">Nama Depan</td>
    <td width="1">:</td>
    <td width="370"><input type="text" name="nama_depan" size="30"/></td>
  </tr>
     <tr>
    <td width="146">Nama Belakang</td>
    <td width="1">:</td>
    <td width="370"><input type="text" name="nama_belakang" size="30"/></td>
  </tr>
  <tr>
    <td>Email</td>
    <td>:</td>
    <td><input type="text" name="email" size="30"/></td>
  </tr>
  <tr>
    <td>Password</td>
    <td>:</td>
    <td><input type="password" name="password" size="30"/></td>
  </tr>
  <tr>
    <td>Kelamin</td>
    <td>:</td>
    <td>
    <select name="kelamin">
   <option value="pria">Pria
   <option value="wanita">Wanita
    </select>
    </td>
  </tr>
  
  <tr>
    <td>Photo</td>
    <td>:</td>
    <td><input type="file" name="photo" size="30"/></td>
  </tr>
  <tr>
    <td colspan="3"><p></p></td>
  </tr>
 
  <tr>
    <td><input type="submit" name="submit" value="Submit" onClick="return confirm('Apakah Anda yakin?')" /></td>
    <td> </td>
    <td><a href="view_data.php">[View Biodata]</a></td>
  </tr>
   </table>   
 </form>

</body>
</html>

3. Membuat View Biodata [view_data.php]
<html>
<head>
<title>View Biodata</title>
</head>
<body>
<?php
include "koneksi.php";
$query=mysql_query("select * from tbl_biodata");
?>

<br>
<table style="font-family:Verdana; size:6px;" align="center" cellspacing="0" cellpadding="3">

<tr bgcolor="#999">
  <td align="center" height="30" colspan="9"><strong><a href="index.php">[Input Biodata]</a></strong></td>
</tr>
<tr bgcolor="#999">
  <td height="30" align="center" style="border-bottom:solid 1px #000000; border-right:solid 1px #000000; border-top:solid 1px #000000; border-left:solid 1px #000000;"><strong>No</strong></td>
  <td align="center" style="border-bottom:solid 1px #000000; border-right:solid 1px #000000; border-top:solid 1px #000000;"><strong>Photo</strong></td>
  <td align="center" style="border-bottom:solid 1px #000000; border-right:solid 1px #000000; border-top:solid 1px #000000;"><strong>Nama</strong></td>
  <td align="center" style="border-bottom:solid 1px #000000; border-right:solid 1px #000000; border-top:solid 1px #000000;"><strong>Jns<br>Kelamin</strong></td>
  <td align="center" style="border-bottom:solid 1px #000000; border-right:solid 1px #000000; border-top:solid 1px #000000;"><strong>Email</strong></td>
  <td align="center" style="border-bottom:solid 1px #000000; border-right:solid 1px #000000; border-top:solid 1px #000000;"><strong>Ukuran</strong></td>
  <td align="center" style="border-bottom:solid 1px #000000; border-right:solid 1px #000000; border-top:solid 1px #000000;"><strong>Type</strong></td>
  <td align="center" style="border-bottom:solid 1px #000000; border-right:solid 1px #000000; border-top:solid 1px #000000;"><strong>Edit</strong></td>
</tr>

<?php
while($row=mysql_fetch_array($query)){
 ?>
 <tr>
  <td align="center" style="border-bottom:solid 1px #000000; border-right:solid 1px #000000; border-left:solid 1px #000000;"><?php echo $no=$no+1;?></td>
  <td style="border-bottom:solid 1px #000000; border-right:solid 1px #000000;"><a href="./photo/<?php echo $row['photo'];?>" target="_blank">
        <img src="./photo/<?php echo $row['photo'];?>" width="100" height="100" title="<?php echo $row['photo'];?>"></a></td>
  <td style="border-bottom:solid 1px #000000; border-right:solid 1px #000000;"><?php echo $row['nama_depan'].' '.$row['nama_belakang'];?></td>
  <td style="border-bottom:solid 1px #000000; border-right:solid 1px #000000;"><?php echo ucwords($row['kelamin']);?></td>
  <td style="border-bottom:solid 1px #000000; border-right:solid 1px #000000;"><?php echo $row['email'];?></td>
  <td style="border-bottom:solid 1px #000000; border-right:solid 1px #000000;"><?php echo formatBytes($row['ukuran'],0);?></td>
  <td style="border-bottom:solid 1px #000000; border-right:solid 1px #000000;"><?php echo $row['type'];?></td>
  <td style="border-bottom:solid 1px #000000; border-right:solid 1px #000000;">
   <a href="delete.php?id_user=<?php echo $row['id_user'];?>" onClick="return confirm('Apakah Anda yakin?')"><strong>Delete</strong></a> |
   <a href="edit.php?id_user=<?php echo $row['id_user'];?>"><strong>Edit</strong></a>
  </td>
 </tr>
 <?php
}
?>
</table>
</body>
</html>

4. Membuat Form Edit [edit.php]
<?
include "koneksi.php";
//jika klik tombol update
if(isset($_POST['submit'])){
 $id_user=$_POST['id_user'];
 $nama_depan=addslashes(htmlentities(ucwords($_POST['nama_depan'])));
 $nama_belakang=addslashes(htmlentities(ucwords($_POST['nama_belakang'])));
 $email=addslashes(htmlentities($_POST['email'])); 
 $kelamin=$_POST['kelamin'];
 $photo_lama=$_POST['photo_lama'];
 $nama_photo=$_FILES['photo']['name'];
 
 if(empty($_POST['password'])){
  $password=$_POST['password_lama'];
 }else{
  $password=$_POST['password'];
 }
  
 if(empty($nama_depan) || empty($nama_belakang) || empty($email)){
  echo "<script language=\"Javascript\">\n";
  echo "confirmed = window.confirm('Maaf, Form belum lengkap!!');";
  echo "</script>";
 }else{  
  if(empty($_FILES['photo']['name'])){
    $nama_file_upload=$photo_lama;
    $type=$_POST['type'];
    $ukuran=$_POST['ukuran'];    
    $query=mysql_query("update tbl_biodata set nama_depan='$nama_depan',nama_belakang='$nama_belakang',email='$email',password='$password',photo='$nama_file_upload',kelamin='$kelamin',ukuran='$ukuran',type='$type' where id_user='$id_user'");
   }else{    
    $type=$_FILES['photo']['type'];
    $ukuran=$_FILES['photo']['size'];  
    $uploaddir='./photo/';
    $rnd=date(His);    
    $nama_file_upload=$rnd.'-'.$nama_photo;
    $alamatfile=$uploaddir.$nama_file_upload;
    
    if($type != "image/gif"  &&  $type != "image/jpg"  && $type != "image/jpeg" && $type != "image/png") {
      echo "<script language=\"Javascript\">\n";
      echo "confirmed = window.confirm('File Yang Di izinkan Hanya jpg,jpeg,png,gif!!');";
      echo "</script>";
    }else{
     if($ukuran>1000000){
      echo "<script language=\"Javascript\">\n";
      echo "confirmed = window.confirm('File Yang Di izinkan Hanya berukuran kurang dari 1MB!!');";
      echo "</script>";
     }else{    
      unlink("./photo/".$photo_lama);
      $upload=move_uploaded_file($_FILES['photo']['tmp_name'],$alamatfile);
      $query=mysql_query("update tbl_biodata set nama_depan='$nama_depan',nama_belakang='$nama_belakang',email='$email',password='$password',photo='$nama_file_upload',kelamin='$kelamin',ukuran='$ukuran',type='$type' where id_user='$id_user'");
     }
    }
   }
   
   if($query){
    echo "<script language=\"Javascript\">\n";
    echo "confirmed = window.confirm('Data Anda berhasil disimpan');";
    echo "</script>";
   }else{
    echo mysql_query();
   }
 }
}else{
 unset($_POST['submit']);
} 
//deskripsi form edit
$id_user=$_GET['id_user'];
$row=mysql_fetch_array(mysql_query("select * from tbl_biodata where id_user='$id_user'"));
$nama_depan=$row['nama_depan'];
$nama_belakang=$row['nama_belakang'];
$email=$row['email'];
$kelamin=$row['kelamin'];
$password=$row['password'];
$photo=$row['photo'];
$ukuran=$row['ukuran'];
$type=$row['type'];


?>
<html>
<head>
 <title>Biodata</title>
</head>
<body>
 <form action="" method="post"  enctype="multipart/form-data" name="postform"> 
    <input type="hidden" name="id_user" value="<?php echo $id_user;?>" />
    <input type="hidden" name="password_lama" value="<?php echo $password;?>" />
    <input type="hidden" name="photo_lama" value="<?php echo $photo?>" />
    <input type="hidden" name="ukuran" value="<?php echo $ukuran;?>" />
    <input type="hidden" name="type" value="<?php echo $type?>" />
 <table width="42%" border="0"  style="font-family:Verdana; size:6px; border-bottom:solid 1px #000000; border-right:solid 1px #000000; border-top:solid 1px #000000; border-left:solid 1px #000000; background:#999" align="center" cellpadding="3" cellspacing="3">
    <tr>
    <td colspan="3" align="center"><strong>FORM EDIT BIODATA</strong></td>
  </tr>
    <tr>
      <td colspan="3" align="center"><img src="./photo/<?php echo $photo;?>" width="100" height="100"></td>
    </tr>
    <tr>
      <td width="189">Nama Depan</td>
      <td width="8">:</td>
      <td width="440"><input type="text" name="nama_depan" value="<?php echo $nama_depan; ?>" size="30"/></td>
    </tr>
      <tr>
      <td width="189">Nama Belakang</td>
      <td width="8">:</td>
      <td width="440"><input type="text" name="nama_belakang" value="<?php echo $nama_belakang; ?>" size="30"/></td>
    </tr>
 <tr>
      <td>Kelamin</td>
      <td>:</td>
      <td>
      <select name="kelamin">
        <option value="pria" <?php if($kelamin=='pria'){ echo "selected='selected'";} ?>>Pria
        <option value="wanita" <?php if($kelamin=='wanita'){ echo "selected='selected'";} ?>>Wanita
      </select>
      </td>
    </tr>
    <tr>
      <td>Email</td>
      <td>:</td>
      <td><input type="text" name="email" value="<?php echo $email; ?>" size="30"/></td>
    </tr>
    <tr>
      <td></td>
      <td></td>
      <td><i><font color="#CCCCCC">*kosongkan password jika tidak diubah</font></i></td>
    </tr>
    <tr>
      <td>Password</td>
      <td>:</td>
      <td><input type="password" name="password" size="30"/></td>
    </tr>   
    <tr>
      <td></td>
      <td></td>
      <td><i><font color="#CCCCCC">*kosongkan photo jika tidak diubah </font></i></td>
    </tr>
    <tr>
      <td>Photo</td>
      <td>:</td>
      <td><input type="file" name="photo" size="30"/></td>
   </tr>
    <tr>
      <td colspan="3"><p></p></td>
    </tr>
    <tr>
      <td><input type="submit" name="submit"  value="Update" onClick="return confirm('Apakah Anda yakin akan mengubah data?')"/></td>
      <td> </td>
   <td><a href="view_data.php"><strong>[View Biodata]</strong></a></td>
 </tr>
    </table>
   
 </form>

</body>
</html>

5. Membuat Proses Delete [delete.php]
<?php
include "koneksi.php";
$id_user=$_GET['id_user'];
$row=mysql_fetch_array(mysql_query("select * from tbl_biodata where id_user='$id_user'"));
$photo=$row['photo'];

$query=mysql_query("delete from tbl_biodata where id_user='$id_user'");
if($query){
 $nama_file="./photo/".$photo;
 unlink($nama_file);
    echo "<script language=\"Javascript\">\n";
    echo "confirmed = window.confirm('Data sudah di hapus!');";
    echo "</script>";
 echo "<a href=view_data.php> <strong>View Data</strong></a>";
}
?>
Selesai, mohon maaf masih banyak kekurangan, namanya juga masih belajar. Oke teman, semoga bermanfaat :)

7 comments:

Anwar Jores

Thanks mbak..ntar d coba..

Anwar Jores

maaf mbak..setelah say coba,,ternyata ukuran, type dan menu edit gk nampil di view_data.php..
solusinya mbak..
terima ksih mbak..

Anwar Jores

Kalu d dlm database ny, ukuran dan type ny trsimpan..tpi di view_data ny ngk nmpil serta link edit ny..

anggun patriana

Bisa saja ditampilin, tapi saran saya ukuran foto itu lebih baik di update dari photonya sendri, kalopun ukurannya kegedean kan sudah di validasi sebelumnya.

$_FILES['file']['type'] => Fungsi ini digunakan untuk mengetahui jenis file yang di upload
$_FILES['file']['name'] => Fungsi ini digunakan untuk mengetahui nama file yang di upload
$_FILES['file']['size'] => Fungsi ini digunakan untuk mengetahui ukuran file yang di upload

Anwar Jores

Trima ksih mbak atas cntohny..
Ad contoh script siswa lihat nilai dgn username yg brbeda.dn sisws tsb tdk bsa lihat nilai teman ny..

Aditya Rizki Pradana

hot, mbak

Yourfri3nd

please code of
koneksi.php

Posting Komentar