Upload gambar/image ke folder upload dengan PHP

1. Buat folder upload dan upload/upload, Membuat index.html di folder upload berfungsi

Berfungsi untuk menampilkan form upload

 HTML (index.html)


<html>
<body>
<form action="uploader.php" method="post" enctype="multipart/form-data">
Filename:
<input type="file" name="file" id="file" />
<br />
<input type="submit" name="submit" value="Submit" />
</form>
</body>
</html>

2. Membuat uploader.php

Berfungsi untuk mengupload file gambar ke dalam folder upload/

PHP (uploader.php)

<?php
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/jpg")
|| ($_FILES["file"]["type"] == "image/png"))
&& ($_FILES["file"]["size"] < 200000))
  {
  if ($_FILES["file"]["error"] > 0)
    {
    echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
    }
  else
    {
    echo "Upload: " . $_FILES["file"]["name"] . "<br />";
    echo "Type: " . $_FILES["file"]["type"] . "<br />";
    echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
    echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />";

    if (file_exists("upload/" . $_FILES["file"]["name"]))
      {
      echo $_FILES["file"]["name"] . " already exists. ";
      }
    else
      {
      move_uploaded_file($_FILES["file"]["tmp_name"],
      "upload/" . $_FILES["file"]["name"]);
      echo "Stored in: <textarea>http://www.persegres.com/upload/". $_FILES["file"]["name"]."</textarea>";
      }
    }
  }
else
  {
  echo "Invalid file";
  }
?>

3.Membuat galeri.php

Menampilkan semua gambar dalam folder upload/

PHP (galeri.php)

<?php
$folder = "upload/";
$handle = opendir($folder);
echo '<table cellspacing="2" cellpadding="5">';
echo '<tr>';
$i = 1;
$fileGambar = array('png', 'jpg', 'jpeg', 'gif');
while(false !== ($file = readdir($handle))){
    $fileAndExt = explode('.', $file);
    if(in_array(end($fileAndExt), $fileGambar)){
        echo '<td style="border:1px solid #000000;" align="center">
            <img src="'.$folder.''.$file.'" width="40" height="40" /><br />

            '.$file.'
<a href="delete.php?file='.$file.'" onClick="location.href("oldwindowURL")">Delete</a> <br />
        </td>';
        if(($i % 4) == 0){
            echo '</tr><tr>';
        }
        $i++;
    }
}
echo '</tr>';
echo '</table>';
?>

4. Membuat delete.php

Menghapus file gambar yang di pilih.

PHP (delete.php)

<?php
$file=$_GET['file'];
$fileName = 'upload/'.$file.'';
$delete=unlink($fileName);
if($delete){
header ("location:galeri.php");
}
else{
echo 'gagal';}
?>



Membuat Halaman User / User Page PHP

Penting Untuk awam :
 - Jika offline, anda harus mempunyai aplikasi PHP seperti wamp / xampp dll.
- Jika wamp letakkan pada folder www, dan Jika Xampp letakkan pada folder htdocs.


1. Membuat Database

CREATE DATABASE login;

CREATE TABLE IF NOT EXISTS `user` (
  `nama` varchar(200) NOT NULL,
  `email` varchar(200) NOT NULL,
  `user` varchar(100) NOT NULL,
  `password` varchar(100) NOT NULL,
  PRIMARY KEY (`user`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

INSERT INTO `user` (`nama`, `email`, `user`, `password`) VALUES
('del', 'edgar@gmail.com', 'del', 'del'),
('halim', 'halim1world@gmail.com', 'helmz', 'halim4ever');

2. Membuat Home Page (index.html)

<!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>
<table align="center">
  <tr>
    <td colspan="2"><strong>Home Page</strong></td>
  </tr>
  <tr>
    <td colspan="2"><a href="register.php" style="text-decoration:none; color:#333;">Register</a></td>
  </tr>
</table>
</body>
</html>

3. Membuat PHP untuk koneksi ke database (php/config.php) \\ didalam folder php

<?php
mysql_connect('localhost','root','');
mysql_select_db('login');
?>

4. Membuat Forum Pendaftaran (register.html)

<!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>Register</title>
</head>
<body><br />
<br />
<form method="post" action="php/register.php">
<table align="center">
  <tr>
    <td colspan="2" align="center"><strong>Register Page</strong><br />
<br /></td>
  </tr>
  <tr>
    <td>Nama</td>
    <td><input type="text" name="nama" id="nama" /></td>
  </tr>
  <tr>
    <td>Email</td>
    <td><input type="text" name="email" id="email" /></td>
  </tr>
  <tr>
    <td>Username</td>
    <td><input type="text" name="user" id="user" /></td>
  </tr>
  <tr>
    <td>Password</td>
    <td><input type="password" name="password" id="password" /></td>
  </tr>
  <tr>
    <td>Re-Password</td>
    <td><input type="password" name="passwordr" id="passwordr" /></td>
  </tr>
  <tr>
  <td></td><td><input type="submit" value="Register" /></td></tr>
</table>
</form>
</body>
</html>

5. Membuat halaman proses Pendaftaran (php/register.php) // di dalam folder php

<?php
include 'config.php';
$nama=$_POST['nama'];
$email=$_POST['email'];
$user=$_POST['user'];
$password=$_POST['password'];
$passwordr=$_POST['passwordr'];

if($password == $passwordr){
$sql='insert into user set nama="'.$nama.'", email="'.$email.'",user="'.$user.'", password="'.$password.'"';
$query=mysql_query($sql);
if($query){
header ("location:login.html");
}
else{
echo 'gagal input database';}
}
else{
echo 'verifikasi salah!';}
?>

6. Membuat login (php/login.html)

<!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>Login Page</title>
</head>
<body>
<form method="post" action="verifikasi.php">
<table align="center">
  <tr>
    <td colspan="2"><strong>Login Page</strong></td>
  </tr>
  <tr>
    <td>Nama</td>
    <td><input type="text" name="user" /></td>
  </tr>
  <tr>
    <td>Password</td>
    <td><input type="password" name="password" /></td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td><input type="submit" value="Login" /></td>
  </tr>
</table>
</form>
</body>
</html>

7. Membuat halaman verifikasi login (php/verifikasi.php)

<?php
include 'config.php';
$sql='select * from user';
$query=mysql_query($sql);
while($rows=mysql_fetch_array($query)){
$user[]=$rows['user']; $password[]=$rows['password'];
}
$user_id=$_POST['user'];
$passwordr=$_POST['password'];

$ok=FALSE;
for($i=0;$i<count($user);$i++){
if($user_id == $user[$i] and $passwordr == $password[$i]){
$ok=TRUE;
break;
}
}
session_start();
if($ok){
$_SESSION['ok']='OK';
$_SESSION['user_id']=$user_id;
header ("location:user.php");
}
?>

8. Membuat Halaman User (php/user.php)

<?php
session_start();
if($_SESSION['ok'] != 'OK'){
header ("location:../");
} // memastikan user sudah login, jika belum akan dialihkan ke halaman register.php
include "config.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>
<table align="center">
  <tr>
    <td width="100"><strong>Hello, <?php echo $_SESSION['user_id']; ?></strong></td> <td><a href="logout.php" style="text-decoration:none; color:#000;">Logout</a></td>
  </tr>
  <?php
  $sql='select * from user where user="'.$_SESSION['user_id'].'" LIMIT 1';
  $query=mysql_query($sql);
  while($rows=mysql_fetch_array($query)){
 echo '<tr><td><br /><br />Nama : </td><td><br /><br />'.$rows['nama'].'</td></tr>
 <tr><td>Email : </td><td>'.$rows['email'].'</td></tr>
 <tr><td>Username : </td><td>'.$rows['user'].'</td></tr>
 <tr><td>Password : </td><td>'.$rows['password'].'</td></tr>';}
  ?> 
</table>
</body>
</html>

9. Membuat Logout (php/logout.php)

<?php
session_start();
session_destroy();
header ("location:../");
?>