Upload Gambar Dengan PHP

Sekarang, kita akan coba untuk upload gambar melalui php, meyimpan gambar tersebut di dalam sebuah folder, dan simpan lokasi folder tersebut di dalam SQL database.
Caranya cukup mudah. Silahkan siapkan SQL database anda dan juga file editor yang biasa anda gunakan.
Sudah dipersiapkan semuanya? Silahkan ikuti cara-cara berikut:
1. Buat Database
Silahkan buat database baru dengan nama "upload2" atau nama lain yang anda inginkan. Setelah itu, silahkan buat table dengan nama "store" atau nama lain yang anda inginkan. Pastikan table "store" anda memiliki struktur sebagai berikut:
Table "store":
CREATE TABLE store (
id INT(11) NOT NULL AUTO_INCREMENT,
name VARCHAR(500) NOT NULL,
location VARCHAR(500) NOT NULL,
PRIMARY KEY (id)
);
2. PHP Files
Anda perlu membuat 4 files PHP:
Begini cara kerjanya. file "index.php" adalah file utama ketika anda akan memilih gambar mana yang akan anda upload. Setelah itu, ketika anda klik submit, file "upload.php" akan bertanggung jawab ketika proses upload dimulai. File "view.php" akan kita gunakan untuk melihat gambar-gambar yang telah di upload. Dan file "edit.php" akan kita gunakan untuk mengganti gambar yang sudah di upload dengan gambar yang baru.
index.php:
<html>

<form action="upload.php" method="POST" enctype="multipart/form-data">
    <input type="file" name="myfile" required accept="image/jpeg"><br/>
    <input type="submit" name="submit" value="Upload">
</form>

</html>

upload.php:
<?php

mysql_connect("localhost","dbuser","dbpwd") or die (mysql_error());
mysql_select_db("upload2") or die (mysql_error());

if($_POST['submit'])
{

    $name=$_FILES['myfile']['name'];
    $type=$_FILES['myfile']['type'];
    $size=$_FILES['myfile']['size'];
    $temp=$_FILES['myfile']['tmp_name'];

    if(!$name)
        {
            die("Error!");
        }
    else
        {
            if($type=="image/jpeg" && $size<102400) //file size is in Bytes
                {
                $timestamp=strtotime("now");
                $file_name="img".$timestamp."-".$name."";
                $location="img/$file_name";
                move_uploaded_file($temp,$location);
                $query=mysql_query("INSERT INTO store (name,location) VALUES ('$file_name','$location')");
                echo "OK!";
                }
            else
                {
                echo "The format of the file is not allowed!";
                }
        }

}

?>

view.php:
<?php
mysql_connect("localhost","dbuser","dbpwd") or die (mysql_error());
mysql_select_db("upload2") or die (mysql_error());

$query = mysql_query("SELECT * FROM store");
  while($row=mysql_fetch_array($query))
  {
    echo $row['name'];
    $location=$row['location'];
    echo "<img src='$location' width='100' height='100'>";
    echo "<br/>";
  }
?>

edit.php:
<html>

<?php
error_reporting(E_ALL ^ E_NOTICE);
mysql_connect("localhost","dbuser","dbpwd") or die (mysql_error());
mysql_select_db("upload2") or die (mysql_error());
?>

<form action="edit.php" method="POST" enctype="multipart/form-data">
    <input type="file" name="myfile" required accept="image/jpeg"><br/>
    <input type="submit" name="submit" value="Upload">
</form>

</html>

<?php
if($_POST['submit'])
{
    $name=$_FILES['myfile']['name'];
    $type=$_FILES['myfile']['type'];
    $size=$_FILES['myfile']['size'];
    $temp=$_FILES['myfile']['tmp_name'];
    
/* WARNING
For the sake of this tutorial, you have to change the id below to the image's id that you want to edit.
Untuk keperluan tutorial ini, anda harus menentukan id gambar mana yang mau anda edit */
    $id=13;

    $query=mysql_query("SELECT * FROM store WHERE id='$id'");
    $row=mysql_fetch_array($query);
    $old_location=$row['location'];
    $delete_file=unlink($old_location);
    
    if($delete_file)
    {
        if(!$name)
        {
            die("Error!");
        }
        else
        {
            if($type=="image/jpeg" && $size<102400) //file size is in Bytes
                {
                $timestamp=strtotime("now");
                $file_name="img".$timestamp."-".$name."";
                $location="img/$file_name";
                move_uploaded_file($temp,$location);
                $query=mysql_query("UPDATE store SET name='$file_name', location='$location' WHERE id='$id'");
                echo "OK!";
                }
                else
                {
                echo "The format of the file is not allowed!";
                }
        }
    }
    else
    {
        echo "Ooops...!";
    }
}
?>

Setelah anda siap, gunakan file "index.php" untuk mulai upload gambar. Setelah itu, gunakan file "view.php" untuk melihat gambar-gambar yang telah di upload. Dan yang terakhir, silahkan gunakan file "edit.php" untuk mengganti gambar yang telah ada dengan gambar yang baru.
Selamat mencoba...!

Labels: , , , , , , ,